tags:

views:

41

answers:

5

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

+3  A: 

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());
}
Fredrik Mörk
You should probably use `IsDigit` rather than `IsNumber`: *"This method [`IsNumber`] determines whether a `Char` is of any numeric Unicode category. In addition to including digits, numbers include characters, fractions, subscripts, superscripts, Roman numerals, currency numerators, and encircled numbers. This method contrasts with the `IsDigit` method, which determines whether a `Char` is a radix-10 digit."* http://msdn.microsoft.com/en-us/library/yk2b3t2y.aspx
LukeH
@LukeH: you are right, thanks! Updated the code sample.
Fredrik Mörk
Thanks for that
StevieB
A: 

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

Onkelborg
A: 

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.

Tim
A: 

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].

Jonas Elfström
A: 

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()); 
dhirschl
What do I need to do to get Regex Working getting this error The name 'Regex' does not exist in the current context
StevieB
using System.Text.RegularExpressions;
dhirschl