关于c#:去掉字符串中的非数字字符

Stripping out non-numeric characters in string

我希望在 ASP.NET C# 中去除字符串中的非数字字符,即 40,595 p.a. 最终会变成 40595.

谢谢


有很多方法,但应该这样做(虽然不知道它在处理非常大的字符串时如何执行):

1
2
3
4
private static string GetNumbers(string input)
{
    return new string(input.Where(c => char.IsDigit(c)).ToArray());
}


感觉很适合正则表达式。

1
2
var s ="40,595 p.a.";
var stripped = Regex.Replace(s,"[^0-9]","");

"[^0-9]" 可以替换为 @"\\D" 但我喜欢 [^0-9] 的可读性。


扩展方法将是更好的方法:

1
2
3
4
5
public static string GetNumbers(this string text)
    {
        text = text ?? string.Empty;
        return new string(text.Where(p => char.IsDigit(p)).ToArray());
    }


1
public static string RemoveNonNumeric(string value) => Regex.Replace(value,"[^0-9]","");


使用仅捕获 0-9 并丢弃其余部分的正则表达式。正则表达式是第一次会花费很多的操作。或者做这样的事情:

1
2
3
4
5
6
7
8
9
var sb = new StringBuilder();
var goodChars ="0123456789".ToCharArray();
var input ="40,595";
foreach(var c in input)
{
  if(goodChars.IndexOf(c) >= 0)
    sb.Append(c);
}
var output = sb.ToString();

我想是这样的,不过我还没有编译..

正如 Fredrik 所说,LINQ 也是一种选择


另一种选择...

1
2
3
4
5
private static string RemoveNonNumberDigitsAndCharacters(string text)
{
    var numericChars ="0123456789,.".ToCharArray();
    return new String(text.Where(c => numericChars.Any(n => n == c)).ToArray());
}


如果您在 VB 中工作并最终来到这里,那么 ".Where" 会为我抛出错误。从这里得到这个:https://forums.asp.net/t/1067058.aspx?Trimming a string to remove special non numeric characters

1
2
3
4
5
6
7
8
9
10
11
12
Function ParseDigits(ByVal inputString as String) As String
  Dim numberString As String =""
  If inputString = Nothing Then Return numberString

  For Each c As Char In inputString.ToCharArray()
    If c.IsDigit Then
      numberString &= c
    End If
  Next c

  Return numberString
End Function

1
 var output = new string(input.Where(char.IsNumber).ToArray());

接受的答案很好,但是它没有考虑 NULL 值,因此在大多数情况下都无法使用。

这促使我改用这些辅助方法。第一个回答了 OP,而其他的可能对那些想要执行相反操作的人有用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    /// <summary>
    /// Strips out non-numeric characters in string, returning only digits
    /// ref.: https://stackoverflow.com/questions/3977497/stripping-out-non-numeric-characters-in-string
    /// </summary>
    /// <param name="input">the input string</param>
    /// <param name="throwExceptionIfNull">if set to TRUE it will throw an exception if the input string is null, otherwise it will return null as well.</param>
    /// <returns>the input string numeric part: for example, if input is"XYZ1234A5U6" it will return"123456"</returns>
    public static string GetNumbers(string input, bool throwExceptionIfNull = false)
    {
        return (input == null && !throwExceptionIfNull)
            ? input
            : new string(input.Where(c => char.IsDigit(c)).ToArray());
    }

    /// <summary>
    /// Strips out numeric and special characters in string, returning only letters
    /// </summary>
    /// <param name="input">the input string</param>
    /// <param name="throwExceptionIfNull">if set to TRUE it will throw an exception if the input string is null, otherwise it will return null as well.</param>
    /// <returns>the letters contained within the input string: for example, if input is"XYZ1234A5U6~()" it will return"XYZAU"</returns>
    public static string GetLetters(string input, bool throwExceptionIfNull = false)
    {
        return (input == null && !throwExceptionIfNull)
            ? input
            : new string(input.Where(c => char.IsLetter(c)).ToArray());
    }

    /// <summary>
    /// Strips out any non-numeric/non-digit character in string, returning only letters and numbers
    /// </summary>
    /// <param name="input">the input string</param>
    /// <param name="throwExceptionIfNull">if set to TRUE it will throw an exception if the input string is null, otherwise it will return null as well.</param>
    /// <returns>the letters contained within the input string: for example, if input is"XYZ1234A5U6~()" it will return"XYZ1234A5U6"</returns>
    public static string GetLettersAndNumbers(string input, bool throwExceptionIfNull = false)
    {
        return (input == null && !throwExceptionIfNull)
            ? input
            : new string(input.Where(c => char.IsLetterOrDigit(c)).ToArray());
    }

有关更多信息,请阅读我博客上的这篇文章。


下面是使用正则表达式的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string str ="40,595 p.a.";

StringBuilder convert = new StringBuilder();

string pattern = @"\\d+";
Regex regex = new Regex(pattern);

MatchCollection matches = regex.Matches(str);

foreach (Match match in matches)
{
convert.Append(match.Groups[0].ToString());
}

int value = Convert.ToInt32(convert.ToString());


嗯,你知道数字是什么:0123456789,对吧?逐个字符遍历你的字符串;如果字符是数字,则将其附加到临时字符串的末尾,否则忽略。可能还有其他可用于 C# 字符串的辅助方法,但这是一种适用于任何地方的通用方法。