如何从字符串中删除字母?

Jaf*_*ffa 5 c# regex

我想去除字符串中的所有字母,这些字母不是数字.优选使用正则表达式或其他东西制作的解决方案.在C#中.怎么做?

Kob*_*obi 5

使用正则表达式:

str = Regex.Replace(str, @"\D+", "");
Run Code Online (Sandbox Code Playgroud)

\D\d- 补充所有不是数字的东西.+将匹配其中一个或多个(它通常比一个一个好一点).

使用Linq(在.Net 4.0上):

str = String.Concat(str.Where(Char.IsDigit));
Run Code Online (Sandbox Code Playgroud)