Hey Im looking to strip out non-numeric characters in a string in ASP.NET C#
So i.e 40,595 p.a.
would end up with 40595
Thanks
Hey Im looking to strip out non-numeric characters in a string in ASP.NET C#
So i.e 40,595 p.a.
would end up with 40595
Thanks
There are many ways, but this should do (don't know how it performs with really large strings though):
private static string GetNumbers(string input)
{
return new string(input.Where(c => char.IsDigit(c)).ToArray());
}
Use either a regular expression that's only capturing 0-9 and throws away the rest. A regular expression is an operation that's going to cost a lot the first time though. Or do something like this:
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();
Something like that I think, I haven't compiled though..
LINQ is, as Fredrik said, also an option
Well, you know what the digits are: 0123456789, right? Traverse your string character-by-character; if the character is a digit tack it onto the end of a temp string, otherwise ignore. There may be other helper methods available for C# strings but this is a generic approach that works everywhere.
Feels like a good fit for a regular expression.
var s = "40,595 p.a.";
var stripped = Regex.Replace(s, "[^0-9]", "");
"[^0-9]"
can be replaced by @"\D"
but I like the readability of [^0-9]
.
Here is the code using Regular Expressions:
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());