将所有字符都放在最后一个破折号的右边 [英] get all characters to right of last dash

查看:101
本文介绍了将所有字符都放在最后一个破折号的右边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

string test = "9586-202-10072"

我如何将所有字符放在最后一个-的右边,所以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.

这怎么办?

推荐答案

您可以使用 str.LastIndexOf('-')-获取最后的位置。因此,下一步很明显:

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

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

更正

如下面的Brian所述,在没有破折号的字符串上使用此字符串将导致返回相同的字符串。

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

这篇关于将所有字符都放在最后一个破折号的右边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆