string formatting

  • Thread starter Thread starter Brian Cahill
  • Start date Start date
B

Brian Cahill

I am attempting to populate a listbox from a text file. I would like
my result to get everything after that last "/" character. This code
gets everything before.

Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
TextLine = TextLine.Substring(0, TextLine.LastIndexOf("/"c))
MsgBox(TextLine)
Loop


Example:
Applications/AS - ED Tracking Board/EDTB Train


I would like it to result to this
EDTB Train


Instead I am getting this:
Applications/AS - ED Tracking Board


thanks
 
TextLine = TextLine.Substring(0, TextLine.LastIndexOf("/"c))

Make that

TextLine = TextLine.Substring(TextLine.LastIndexOf("/"c) + 1)

Note that it will fail if the / is the last character of the string,
so you may want to add a check for that.


Mattias
 
Brian Cahill said:
perfect. thanks a bunch. I don't understand the logic though. What
does the +1 do?

It just adds one to the index - otherwise you'll get the string from
the "/" character onwards, instead of from the first character *after*
the slash.
 
Here is line I am editing: "Applications/AS - ED Tracking Board/EDTB
Train"

But when I do TextLine = TextLine.Substring(TextLine.LastIndexOf("/"c))
I get "Applications/AS - ED Tracking Board"

And when I add the plus 1 TextLine =
TextLine.Substring(TextLine.LastIndexOf("/"c) + 1)
I get "EDTB Train"
From what you are saying, without the plus 1, I should get get "/EDTB
Train"
 
Back
Top