C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: Conversely the "\D" metacharacter matches non-digit characters. Uppercase means "not."
Regex.ReplaceSo: The string "6cylinder" will become the string "cylinder". The method works correctly on strings that have digits in any part.
RemoveDigits: RemoveDigits is used to remove the numeric characters from the input string.
Tip: The "\d" regex pattern string specifies a single digit character (0 through 9).
And: This code will not match negative numbers of numbers with decimal places. The @ character designates a verbatim string literal.
C# program that removes numbers
using System;
using System.Text.RegularExpressions;
class Program
{
/// <summary>
/// Remove digits from string.
/// </summary>
public static string RemoveDigits(string key)
{
return Regex.Replace(key, @"\d", "");
}
static void Main()
{
string input1 = "Dot123Net456Perls";
string input2 = "101Dalmatations";
string input3 = "4 Score";
string value1 = RemoveDigits(input1);
string value2 = RemoveDigits(input2);
string value3 = RemoveDigits(input3);
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(value3);
}
}
Output
DotNetPerls
Dalmatations
Score
Then: The Regex allocates new string data on the managed heap and returns a reference to that object data.
Finally: You could return that character array as a string using the new string constructor.
String ConstructorFinally: We explored performance issues and other tasks related to numbers in strings.
Regex