string question

  • Thread starter Thread starter shane
  • Start date Start date
S

shane

I have gone over allot of the samples and documentation on the internet on
strings. I am not seeing a good example of a function or sub to remove all
characters to right or left of a given character. The ones I see I don't
know how to implement.

I want to take the following

c:\temp\otherTemp
c:\temp\2ndTemp

and just returns from what follow after the last"\"

otherTemp
2ndTemp

I see the String.TrimStart Method...but i cant figure out how to implement
it.

thanks
 
Hi

Dim sPath As String = "c:\temp\otherTemp"
Dim sResult=sPath.Substring(sPath.LastIndexOf("\") + 1)

Mex
 
shane said:
I want to take the following

and just returns from what follow after the last"\"

Since you're working with file system Paths, how about using the Path
class in System.IO?

Imports System.IO

? Path.GetFilename( "c:\temp\otherTemp" )

Or, if you're determined to do it the String way ...

Dim s as String = "c:\temp\otherTemp"
? s.SubString( s.LastIndexof( "\" ) + 1 )

.... but don't be surprised if you get odd results from this, from time
to time. The methods in Path are far more tolerant of dodgy-looking
paths.

HTH,
Phill W.
 
shane said:
I have gone over allot of the samples and documentation on the internet on
strings. I am not seeing a good example of a function or sub to remove all
characters to right or left of a given character. The ones I see I don't
know how to implement.

I want to take the following

c:\temp\otherTemp
c:\temp\2ndTemp

and just returns from what follow after the last"\"

otherTemp
2ndTemp

I see the String.TrimStart Method...but i cant figure out how to implement
it.

thanks
Since you asked for a String solution..
broken down into steps:
Dim s as String = "c:\temp\otherTemp"
Dim pos as Integer = s.lastIndexOf("\") + 1
Dim strFileName as String = s.substring(pos )

You may combine a couple of steps
HTH
 
Back
Top