IsNumeric question

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi,
I am trying to check a string to see if it's first 3 characters are numeric
and if they are, to replace those 3 characters with something else.

I've tried this but nothing happens...

newname = Replace(newname, VB.Left(newname, 3) = "101-", "101. ")



The method I've been using up to now is very drawn out and too long and I
need a more efficient way of doing it.
This is what I've beeni using...

If VB.Left(newname, 3) = "01-" Or VB.Left(newname, 3) = "02-" _
Or VB.Left(newname, 3) = "03-" Or VB.Left(newname, 3) = "04-" _
Or VB.Left(newname, 3) = "05-" Or VB.Left(newname, 3) = "06-" _
Or VB.Left(newname, 3) = "07-" Or VB.Left(newname, 3) = "08-" _
Or VB.Left(newname, 3) = "09-" Or VB.Left(newname, 3) = "10-" Then
newname = Replace(newname, "01-", "01. ")
newname = Replace(newname, "02-", "02. ")
newname = Replace(newname, "03-", "03. ")
newname = Replace(newname, "04-", "04. ")
newname = Replace(newname, "05-", "05. ")
newname = Replace(newname, "06-", "06. ")
newname = Replace(newname, "07-", "07. ")
newname = Replace(newname, "08-", "08. ")
newname = Replace(newname, "09-", "09. ")
newname = Replace(newname, "10-", "10. ")
End If



Any ideas?
Cheers,
Paul
 
I have no idea what you are trying to say, but I'll still try to answer
you.

Are you just trying to replace the first hyphen with a period? If so
just do this:

newname = newname.Substring(0, 3) & Replace(newname.Substring(3, 1),
"-", ".") & newname.Substring(4, newname.Length - 4)

If you need the first 3 characters to be numeric then wrap the above in
an if...then test

i.e.

If IsNumeric(newname.Substring(1, 3)) Then
newname = newname.Substring(0, 3) & Replace(newname.Substring(3,
1), "-", ".") & newname.Substring(4, newname.Length - 4)
MsgBox(newname)
End If

Thanks,

Seth Rowe
 
Paul,

The substring is your friend.

If IsNumeric("123A".Substring(0, 3)) Then
MessageBox.Show("Yes I am")
End If

I hope this helps,

Cor
 
You could also try using the Regular Expression (Regex) features, as they
would let you examine the source string using wildcard-like patterns.
 
If IsNumeric(newname.Substring(1, 3)) Then
newname = newname.Substring(0, 3) & Replace(newname.Substring(3,
1), "-", ".") & newname.Substring(4, newname.Length - 4)
MsgBox(newname)
End If

Replace that with this:

If IsNumeric(newname.Substring(0, 3)) Then
newname = newname.Substring(0, 3) & Replace(newname.Substring(3,
1), "-", ".") & newname.Substring(4, newname.Length - 4)
MsgBox(newname)
End If

Thanks,

Seth Rowe
 
Paul said:
Hi,
I am trying to check a string to see if it's first 3 characters are numeric
and if they are, to replace those 3 characters with something else.

I've tried this but nothing happens...

newname = Replace(newname, VB.Left(newname, 3) = "101-", "101. ")
<snip>

You're asking VB to replace all occurrences of the string "false" by
"101.". That's because the expression VB.Left(newname, 3) = "101-" will
always return False (even though newname actually begins with "101-").
I'm sure it's not what you want.

What it seems you want is to replace the given prefix ("101-") by
another one... one that preserves the first two (or three?) integers
and replaces only the separator (from "-" to ".")

One possible approach could be:

<aircode>
Dim Sz as Integer = 3 'Change this to 2, for names like "10-..."
Dim Test As Integer
Dim Prefix As String = newname.Substring(0, Sz)
If newname(Sz) = "-" AndAlso Integer.TryParse(Prefix, Test) Then
newname = Prefix & "-" & newname.Substring(Sz+1)
End If
</aircode>

HTH.

Regards,

Branco.
 
I apoligise for not explaining myself but whilst writing the post I
accidently pressed the Alt+S key and sent the unfinished message!

Despite this your answer is working but strangely, only partly.

If I use the code as you gave it me, it works fine and replaces the "-" with
a "." but if I alter the code to change "_" to "." it strangely replaces the
"_" with a space instead of a "."
Some strings I have need the "-" replacing and some need the "_" replacing.

So to clarify, if I use the code as you gave it me the string ends up like
this...
"101.this_is_the_filename.mp3" <--works fine

But if I change the code to replace the "_" with a "." I get...
"101 this_is_the_filename.mp3" <--hmm, I get a space instead of a "."

Any ideas why its doing that?

Cheers,
Paul
 
Please post the modified code you are using, including the string you
are trying to parse so we can see what you're doing.

Thanks,

Seth Rowe
 
Sorry again, I posted my second message after you had provided me with the
replacement code.
It works fine now!

What I have now is...

If IsNumeric(newname.Substring(0, 3)) Then
newname = newname.Substring(0, 3) & Replace(newname.Substring(3, 1),
"_", ". ") & newname.Substring(4, newname.Length - 4)
'MsgBox(newname)
End If

If IsNumeric(newname.Substring(0, 3)) Then
newname = newname.Substring(0, 3) & Replace(newname.Substring(3, 1),
"-", ". ") & newname.Substring(4, newname.Length - 4)
'MsgBox(newname)
End If


Is there any way I can incorporate both into one routine? I mean replace
the "-" and the "_" to "." in one go?


Cheers,
Paul
 
Sorry again, I posted my second message after you had provided me with the
replacement code.

Yeah, sorry about the typo in the first post - I tend to type before I
think sometimes :-)

Try this:

If IsNumeric(newname.Substring(0, 3)) Then
Dim temp As String = newname.Substring(3, 1)
If temp = "-" OrElse temp = "_" Then ' Add more OrElse statements
to replace other characters if required
newname = newname.Substring(0, 3) &
Replace(newname.Substring(3, 1), temp, ". ") & newname.Substring(4,
newname.Length - 4)
MsgBox(newname)
End If
End If


Thanks,

Seth Rowe
 
use regex class
pattern like "?<prefx>(^\d\d\d[-])"
Caution, I writing from memory and have not check or test the above pattern.

check the built-in help for regex
 
Back
Top