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

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

谢谢

130308 次浏览

有很多种方法,但是这个应该可以(但是不知道如何使用真正大的字符串执行) :

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

使用一个只捕获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 也是一种选择

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

感觉很适合正则表达式。

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

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

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

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

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

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

另一个选择。

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

接受的答案是很好的,但是它没有考虑 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());
}

如需更多信息,请登录我的博客 读读这篇文章

 var output = new string(input.Where(char.IsNumber).ToArray());
public static string RemoveNonNumeric(string value) => Regex.Replace(value, "[^0-9]", "");

如果你在 VB 工作,最终来到了这里。“哪里”为我抛出了一个错误。接下来是 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

从 C # 3.0开始,您应该尽可能使用方法组来代替 lambda 表达式。这是因为使用方法组可以减少重定向,从而提高效率。

因此,目前公认的答案将是:

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

去掉所有非数字字符

Public Function OnlyNumeric(strIn As String) As String
Try
Return Regex.Replace(strIn, "[^0-9]", "")
Catch
Return String.Empty
End Try
End Function