Reputation: 196539
I have the following string:
string test = /test/test1/tse3/ttese3/test3-45-NameToParseOut
and i need to parse out the word "NameToParseOut". I basically need to find the last "-" and return all of the text after the last instance of "-". What is the most elegant way of parsing this out in C#?
Upvotes: 3
Views: 193
Reputation: 12440
Try:
string test = "/test/test1/tse3/ttese3/test3-45-NameToParseOut";
int index = test.LastIndexOf('-');
string value;
if(index != -1) {
value = test.Substring(index) + 1;
}
Read more on LastIndexOf(char) here: http://msdn.microsoft.com/en-us/library/aa904293%28v=VS.71%29.aspx
Upvotes: 2
Reputation: 726599
string test = "/test/test1/tse3/ttese3/test3-45-NameToParseOut";
test = test.Substring(test.LastIndexOf('-')+1);
This works even for strings that do not contain dashes (in these cases the entire string is returned).
Upvotes: 5