How to get the last part of a string?

Viewed 90500

Given this string:

http://s.opencalais.com/1/pred/BusinessRelationType

I want to get the last part of it: "BusinessRelationType"

I have been thinking about reversing the whole string then looking for the first "/", take everything to the left of that and reverse that. However, I'm hoping there is a better/more concise method. Thoughts?

Thanks, Paul

12 Answers

The accepted answer might give undesidered results (empty string) if the url ends with /

To prevent this you can use:

string lastPart = text.TrimEnd('/').Split('/').Last();

Perhaps you could attempt to also do:

string x = "http://s.opencalais.com/1/pred/BusinessRelationType"
string.IsNullOrWhiteSpace(x)?x.Split('/').Last(): x,
Related