Remove all non numeric characters from a string in C#
To remove all non numeric characters from a string in C# we can use regex expression methodRegex.Replace()
and pass the string as first parameter and the expression [^.0-9]
as second parameter and ""
(empty string) as third parameter.Example
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string message = "hello 1 user";
string newString = Regex.Replace(message, "[^.0-9]", "");
Console.WriteLine(newString);
}
}
Output
1