Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split on second occurrence

Status
Not open for further replies.

soulpumpkin

Programmer
Feb 18, 2005
79
US
I have a string, path name, like this:
"c:\foo\Data Codes Mar 2004 - only codes.xls - 9/12/2004 10:23:12"

I need to parse this guy so that I have the following:
"c:\foo\Data Codes Mar 2004 - only codes.xls"

Any help would be great.

Soul Pumpkin
 
newstring=originalstring(0,originalstring.lastindexof("-"))

or something similar
 
There's quite a few ways of doing it. One would be:
Code:
        Dim s As String = "c:\foo\Data Codes Mar 2004 - only codes.xls - 9/12/2004 10:23:12"
        s = s.Substring(0, s.LastIndexOf("-") - 1)

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
sorry...forget the substring bit

newstring=originalstring.substring(0,originalstring.lastindexof("-"))
 
Oops...sorry for the cross-post!

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
ha

I forgot the -1 too. I can never remember if it's -1 for the beginning substring or the end substring
 
Why not just use the Split method?

Code:
NewString = OldString.Split("-")(2)

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Whoops, you wanted the first two parts, not the third.

Code:
dim s() as string = OldString.Split("-")
NewString = s(0) & "-" & s(1)

But I would go for the already posted substring method for it instead.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top