剥去字符串中的非数字字符

时间:2010-10-20 11:56:47

标签: asp.net

嘿我想在ASP.NET C#中删除字符串中的非数字字符

所以即40,595 p.a。

最终会以40595

结束

由于

11 个答案:

答案 0 :(得分:182)

有很多方法,但这应该做(不知道它是如何用非常大的字符串执行的):

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

答案 1 :(得分:43)

感觉非常适合正则表达。

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

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

答案 2 :(得分:6)

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

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

答案 3 :(得分:5)

使用仅捕获0-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也是一个选项

答案 4 :(得分:3)

另一种选择......

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

答案 5 :(得分:3)

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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

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()); 

答案 8 :(得分:0)

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

这促使我使用这些辅助方法。第一个答案是OP,而其他人可能对那些想要表现相反的人有用:

    /// <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());
    }

有关其他信息,请访问我的博客{/ 3}}。

答案 9 :(得分:0)

如果您在VB中工作并在这里结束,则“ .Where”对我来说是一个错误。从这里得到这个:https://forums.asp.net/t/1067058.aspx?Trimming+a+string+to+remove+special+non+numeric+characters

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

答案 10 :(得分:-1)

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