Stripping out non-numeric characters in string in C#
There are many ways to strip out non-numeric characters in string, below is an example, we created a function calledGetNumbers()
which takes input of string
then we use Where()
method and loop through characters and return only char.IsDigit()
, the result will be collected to an array and then casted into a string.using System;
using System.Linq;
public class Program
{
private static string GetNumbers(string input)
{
return new string(input.Where(c => char.IsDigit(c)).ToArray());
}
public static void Main()
{
Console.WriteLine(GetNumbers("11a22b33c44d"));
}
}