extract after second _

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have this string 111_XYZ11_2345, 22_zz22_33, .., How can I extract only
last numbers like 2345 and 33.
Thanks,
Jim.
 
Hi,
I used this one, do you see any problem with this?
lastDashPos = fName.LastIndexOf("_") + 1
LastNumStr = Microsoft.VisualBasic.Right(fName, Len(fName) -
lastDashPos)

Thanks,
Jim.
 
JIM.H. said:
I have this string 111_XYZ11_2345, 22_zz22_33, .., How can I extract only
last numbers like 2345 and 33.

Are you asking for "everything after the last _" or "everything after
the second _"? It makes a difference when you've got more than 2 _
characters in the input string :)

To get everything after the second _, use String.IndexOf twice,
starting the search after the first _ to find the second _. Then use
String.Substring.

To get everything after the last _, use String.LastIndexOf, and then
use String.Substring.

In each case, you should check the result of String.IndexOf - if it's
-1, the _ character wasn't found.
 
Jim,

I do not see your code as a problem, however it does not look nice with
those two namespaces mixed up.
\\\
lastDashPos = fName.LastIndexOf("_")
LastNumStr = fName.SubString(lastDashPosh + 1)
///
I find this looks nicer.

It can even be just as you wish
\\\
LastNumStr = fName.SubString(fName.LastIndexOf("_") +1)
///

Did you know that there is a special and very active newsgroup for VBNet
language questions?

Microsoft.public.dotnet.languages.vb

I hope this helps?

Cor
 
Back
Top