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

string formatting

P: n/a
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))
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

Mar 20 '06 #1
Share this Question
Share on Google+
4 Replies

P: n/a
> 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 Sjögren [C# MVP] mattias @ |
Please reply only to the newsgroup.
Mar 20 '06 #2

P: n/a
perfect. thanks a bunch. I don't understand the logic though. What
does the +1 do?

Mar 20 '06 #3

P: n/a
Brian Cahill <bc*****> wrote:
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.

Jon Skeet - <sk***> Blog:
If replying to the group, please do not mail me too
Mar 20 '06 #4

P: n/a
Here is line I am editing: "Applications/AS - ED Tracking Board/EDTB

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


Mar 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.