user380432 user380432 - 1 year ago 92
C# Question

get all characters to right of last dash

I have the following:

string test = "9586-202-10072"

How would I get all characters to the right of the final
so 10072. The number of characters is always different to the right of the last dash.

How can this be done?

Jon Jon
Answer Source

You can get the position of the last - with str.LastIndexOf('-'). So the next step is obvious:

var result = str.Substring(str.LastIndexOf('-') + 1);


As Brian states below, using this on a string with no dashes will result in the same string being returned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download