C# 只保留字符串中的数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19167669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keep only numeric value from a string?
提问by meda
I have some strings like this
我有一些这样的字符串
string phoneNumber = "(914) 395-1430";
I would like to strip out the parethenses and the dash, in other word just keep the numeric values.
我想去掉括号和破折号,换句话说,只保留数值。
So the output could look like this
所以输出看起来像这样
9143951430
How do I get the desired output ?
如何获得所需的输出?
采纳答案by Nicholas Carey
You do any of the following:
您执行以下任一操作:
Use regular expressions. You can use a regular expression with either
A negative character class that defines the characters that are what you don't want (those characters other than decimal digits):
private static readonly Regex rxNonDigits = new Regex( @"[^\d]+");
In which case, you can do take either of these approaches:
// simply replace the offending substrings with an empty string private string CleanStringOfNonDigits_V1( string s ) { if ( string.IsNullOrEmpty(s) ) return s ; string cleaned = rxNonDigits.Replace(s, "") ; return cleaned ; } // split the string into an array of good substrings // using the bad substrings as the delimiter. Then use // String.Join() to splice things back together. private string CleanStringOfNonDigits_V2( string s ) { if (string.IsNullOrEmpty(s)) return s; string cleaned = String.Join( rxNonDigits.Split(s) ); return cleaned ; }
a positive character set that defines what you do want (decimal digits):
private static Regex rxDigits = new Regex( @"[\d]+") ;
In which case you can do something like this:
private string CleanStringOfNonDigits_V3( string s ) { if ( string.IsNullOrEmpty(s) ) return s ; StringBuilder sb = new StringBuilder() ; for ( Match m = rxDigits.Match(s) ; m.Success ; m = m.NextMatch() ) { sb.Append(m.Value) ; } string cleaned = sb.ToString() ; return cleaned ; }
You're not required to use a regular expression, either.
You could use LINQ directly, since a string is an
IEnumerable<char>
:private string CleanStringOfNonDigits_V4( string s ) { if ( string.IsNullOrEmpty(s) ) return s; string cleaned = new string( s.Where( char.IsDigit ).ToArray() ) ; return cleaned; }
If you're only dealing with western alphabets where the only decimal digits you'll see are ASCII, skipping
char.IsDigit
will likely buy you a little performance:private string CleanStringOfNonDigits_V5( string s ) { if (string.IsNullOrEmpty(s)) return s; string cleaned = new string(s.Where( c => c-'0' < 10 ).ToArray() ) ; return cleaned; }
Finally, you can simply iterate over the string, chucking the digits you don't want, like this:
private string CleanStringOfNonDigits_V6( string s ) { if (string.IsNullOrEmpty(s)) return s; StringBuilder sb = new StringBuilder(s.Length) ; for (int i = 0; i < s.Length; ++i) { char c = s[i]; if ( c < '0' ) continue ; if ( c > '9' ) continue ; sb.Append(s[i]); } string cleaned = sb.ToString(); return cleaned; }
Or this:
private string CleanStringOfNonDigits_V7(string s) { if (string.IsNullOrEmpty(s)) return s; StringBuilder sb = new StringBuilder(s); int j = 0 ; int i = 0 ; while ( i < sb.Length ) { bool isDigit = char.IsDigit( sb[i] ) ; if ( isDigit ) { sb[j++] = sb[i++]; } else { ++i ; } } sb.Length = j; string cleaned = sb.ToString(); return cleaned; }
使用正则表达式。您可以使用正则表达式
一个负字符类,用于定义您不想要的字符(十进制数字以外的字符):
private static readonly Regex rxNonDigits = new Regex( @"[^\d]+");
在这种情况下,您可以采取以下任一方法:
// simply replace the offending substrings with an empty string private string CleanStringOfNonDigits_V1( string s ) { if ( string.IsNullOrEmpty(s) ) return s ; string cleaned = rxNonDigits.Replace(s, "") ; return cleaned ; } // split the string into an array of good substrings // using the bad substrings as the delimiter. Then use // String.Join() to splice things back together. private string CleanStringOfNonDigits_V2( string s ) { if (string.IsNullOrEmpty(s)) return s; string cleaned = String.Join( rxNonDigits.Split(s) ); return cleaned ; }
定义您想要的内容的正字符集(十进制数字):
private static Regex rxDigits = new Regex( @"[\d]+") ;
在这种情况下,您可以执行以下操作:
private string CleanStringOfNonDigits_V3( string s ) { if ( string.IsNullOrEmpty(s) ) return s ; StringBuilder sb = new StringBuilder() ; for ( Match m = rxDigits.Match(s) ; m.Success ; m = m.NextMatch() ) { sb.Append(m.Value) ; } string cleaned = sb.ToString() ; return cleaned ; }
您也不需要使用正则表达式。
您可以直接使用 LINQ,因为字符串是一个
IEnumerable<char>
:private string CleanStringOfNonDigits_V4( string s ) { if ( string.IsNullOrEmpty(s) ) return s; string cleaned = new string( s.Where( char.IsDigit ).ToArray() ) ; return cleaned; }
如果您只处理西方字母,其中您将看到的唯一十进制数字是 ASCII,那么跳过
char.IsDigit
可能会给您带来一点性能:private string CleanStringOfNonDigits_V5( string s ) { if (string.IsNullOrEmpty(s)) return s; string cleaned = new string(s.Where( c => c-'0' < 10 ).ToArray() ) ; return cleaned; }
最后,您可以简单地遍历字符串,删除您不想要的数字,如下所示:
private string CleanStringOfNonDigits_V6( string s ) { if (string.IsNullOrEmpty(s)) return s; StringBuilder sb = new StringBuilder(s.Length) ; for (int i = 0; i < s.Length; ++i) { char c = s[i]; if ( c < '0' ) continue ; if ( c > '9' ) continue ; sb.Append(s[i]); } string cleaned = sb.ToString(); return cleaned; }
或这个:
private string CleanStringOfNonDigits_V7(string s) { if (string.IsNullOrEmpty(s)) return s; StringBuilder sb = new StringBuilder(s); int j = 0 ; int i = 0 ; while ( i < sb.Length ) { bool isDigit = char.IsDigit( sb[i] ) ; if ( isDigit ) { sb[j++] = sb[i++]; } else { ++i ; } } sb.Length = j; string cleaned = sb.ToString(); return cleaned; }
From a standpoint of clarity and cleanness of code, the version 1 is what you want. It's hard to beat a one liner.
从代码的清晰度和清洁度的角度来看,版本 1 就是您想要的。很难击败单班轮。
If performance matters, my suspicion is that the version 7, the last version, is the winner. It creates one temporary — a StringBuilder()
and does the transformation in-place within the StringBuilder's in-place buffer.
如果性能很重要,我怀疑版本 7,即最后一个版本,是赢家。它创建了一个临时的 — aStringBuilder()
并在 StringBuilder 的就地缓冲区中进行就地转换。
The other options all do more work.
其他选项都做更多的工作。
回答by COLD TOLD
use reg expression
使用reg表达式
string result = Regex.Replace(phoneNumber, @"[^\d]", "");
回答by L.B
string phoneNumber = "(914) 395-1430";
var numbers = String.Join("", phoneNumber.Where(char.IsDigit));
回答by Darka
He means everything @gleng
他意味着一切@glen
Regex rgx = new Regex(@"\D");
str = rgx.Replace(str, "");
回答by BRAHIM Kamel
try something like this
尝试这样的事情
return new String(input.Where(Char.IsDigit).ToArray());
回答by Guffa
Instead of a regular expression, you can use a LINQ method:
您可以使用 LINQ 方法代替正则表达式:
phoneNumber = String.Concat(phoneNumber.Where(c => c >= '0' && c <= '9'));
or:
或者:
phoneNumber = String.Concat(phoneNumber.Where(Char.IsDigit));