Odpowiedzi:
Jest wiele sposobów, ale to powinno wystarczyć (nie wiem jednak, jak to działa z naprawdę dużymi ciągami znaków):
private static string GetNumbers(string input)
{
return new string(input.Where(c => char.IsDigit(c)).ToArray());
}
IsDigitraczej użyć niż IsNumber: "Ta metoda [ IsNumber] określa, czy a Charnależy do dowolnej numerycznej kategorii Unicode. Oprócz cyfr, liczby obejmują znaki, ułamki, indeksy dolne, indeksy górne, cyfry rzymskie, liczniki walut i liczby zakreślone. Ta metoda kontrastuje z IsDigitmetodą, która określa, czy a Charjest cyfrą dziesiętną ”. msdn.microsoft.com/en-us/library/yk2b3t2y.aspx
input.Where(c => char.IsDigit(c) || char.IsWhiteSpace(c))
return new string(input.Where(char.IsDigit).ToArray());. Po prostu
Wygląda na to, że dobrze pasuje do wyrażenia regularnego.
var s = "40,595 p.a.";
var stripped = Regex.Replace(s, "[^0-9]", "");
"[^0-9]"można zastąpić, @"\D"ale podoba mi się czytelność [^0-9].
Metoda rozszerzenia będzie lepszym podejściem:
public static string GetNumbers(this string text)
{
text = text ?? string.Empty;
return new string(text.Where(p => char.IsDigit(p)).ToArray());
}
if (text == null) return string.Empty;więcej text = text ?? string.Empty;. W ten sposób nie zmniejszamy wydajności.
Użyj dowolnego wyrażenia regularnego, które przechwytuje tylko 0-9, a resztę odrzuca. Jednak wyrażenie regularne to operacja, która za pierwszym razem będzie dużo kosztować. Lub zrób coś takiego:
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();
Myślę, że coś takiego nie skompilowałem ...
LINQ jest, jak powiedział Fredrik, również opcją
Inna opcja ...
private static string RemoveNonNumberDigitsAndCharacters(string text)
{
var numericChars = "0123456789,.".ToCharArray();
return new String(text.Where(c => numericChars.Any(n => n == c)).ToArray());
}
public static string RemoveNonNumeric(string value) => Regex.Replace(value, "[^0-9]", "");
Oto kod wykorzystujący wyrażenia regularne:
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());
Przyjęta odpowiedź jest świetna, jednak nie bierze pod uwagę wartości NULL, przez co jest bezużyteczna w większości scenariuszy.
To skłoniło mnie do korzystania z tych metod pomocniczych. Pierwsza odpowiada na PO, podczas gdy inne mogą być przydatne dla tych, którzy chcą zrobić coś odwrotnego:
/// <summary>
/// Strips out non-numeric characters in string, returning only digits
/// ref.: /programming/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());
}
Aby uzyskać dodatkowe informacje, przeczytaj ten post na moim blogu.
var output = new string(input.Where(char.IsNumber).ToArray());