从字符串获取所有数字的最佳方法 [英] Best way to get all digits from a string

查看:117
本文介绍了从字符串获取所有数字的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么更好的办法让一个字符串,如(123)455-2344,并获得1234552344,从比这样做:

Is there any better way to get take a string such as "(123) 455-2344" and get "1234552344" from it than doing this:

var matches = Regex.Matches(input, @"[0-9]+", RegexOptions.Compiled);

return String.Join(string.Empty, matches.Cast<Match>()
                                .Select(x => x.Value).ToArray());



也许这可以做到在一场比赛中的正则表达式?我似乎无法创建一个实现,虽然。

Perhaps a regex pattern that can do it in a single match? I couldn't seem to create one to achieve that though.

推荐答案

你需要使用正则表达式?

Do you need to use a Regex?

return new String(input.Where(Char.IsDigit).ToArray());

这篇关于从字符串获取所有数字的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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