get all characters to right of last dash

前端 未结 9 409
遇见更好的自我
遇见更好的自我 2020-12-07 13:50

I have the following:

string test = \"9586-202-10072\"

How would I get all characters to the right of the final - so 10072. Th

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 14:10

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

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

    Correction:

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

提交回复
热议问题