By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

extract after second _

P: n/a
Hi,
I have this string 111_XYZ11_2345, 22_zz22_33, .., How can I extract only
last numbers like 2345 and 33.
Thanks,
Jim.

Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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." wrote:
Hi,
I have this string 111_XYZ11_2345, 22_zz22_33, .., How can I extract only
last numbers like 2345 and 33.
Thanks,
Jim.

Jul 21 '05 #2

P: n/a
JIM.H. <JI**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

P: n/a
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
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.