?
Solved

Get rightmost token of string

Posted on 2009-04-28
7
Medium Priority
?
359 Views
Last Modified: 2012-05-06
In VB.NET, how do I get the rightmost token of a string, in a period delimited string.  (Could have 1 or more periods.)

For example:
xxxxx.1234.356.34.abc should return abc
fffff.5525.2 should return 2
2.4 should return 4

(I'm really trying to extract the file extension from a filename that could have a period in the name of the file.)

Thanks
0
Comment
Question by:RobertNZana
  • 4
  • 2
7 Comments
 
LVL 39

Accepted Solution

by:
abel earned 1200 total points
ID: 24253541
There are many ways. Here's one:

Dim s As String = "xxxxx.1234.356.34.abc"Dim newString As String = s.Substring(s.LastIndexOf(".") + 1)

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24253545
You could try something like this:

Dim str1 As String = "xxxxx.1234.356.34.abc"
        Dim str2 As String = "fffff.5525.2"
        Dim str3 As String = "2.4"
        Dim str4 As String = "4"

        MsgBox(str1.Substring(str1.LastIndexOf(".") + 1))
        MsgBox(str2.Substring(str2.LastIndexOf(".") + 1))
        MsgBox(str3.Substring(str3.LastIndexOf(".") + 1))
        MsgBox(str4.Substring(str4.LastIndexOf(".") + 1))
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 800 total points
ID: 24253547
How about using System.IO.Path.GetExtension("filename.ext") ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Assisted Solution

by:abel
abel earned 1200 total points
ID: 24253580
Easier perhaps, and it would be clearer for anyone else who has a look at your code, is the following:

Dim newString as String = Path.GetExtension("xxxxx.1234.356.34.abc").Substring(1)
The Substring(1) at the end is used to get rid of the dot, which is returned by the GetExtension.
0
 
LVL 39

Expert Comment

by:abel
ID: 24253588
ah, sorry kaufmed, I didn't refresh while typing... ;)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24253649
:)

I'm starting to think all you do all day is answer EE ?s  ;)
0
 
LVL 39

Expert Comment

by:abel
ID: 24253848
> I'm starting to think all you do all day is answer EE ?s

haha, funny, I've served up four clients, remote and locally, today, been sailing and played my piano so... no, not always, but some days i'm a bit busier then other days... ;-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question