Regex.Replace, digits. A C# Regex can match numeric characters. This helps with certain requirements. Some programs need to remove digit character to clean up input data.
Metacharacters. The "\d" metacharacter matches digit characters. Conversely the "\D" metacharacter matches non-digit characters. Uppercase means "not."
In this example, the characters with the exact values 0 through 9 must be removed from the string. They are not replaced with other characters but entirely removed.
So The string "6cylinder" will become the string "cylinder". The method works correctly on strings that have digits in any part.
Detail 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 "at" character designates a verbatim string literal.
Input and output. The 3 strings have their numbers removed. Internally, RemoveDigits receives a reference to the input strings, which would be interned in the runtime.
Then The Regex allocates new string data on the managed heap and returns a reference to that object data.
Performance. The performance of this method is far from ideal. For more speed, you could use a char array and dynamically build up the output string.
Finally You could return that character array as a string using the new string constructor.
Discussion. There are many possible requirements when dealing with numbers embedded in strings. Several useful methods are available in .NET, such as char.IsDigit and int.TryParse.
Summary. You can remove number characters in strings using the C# language. This requirement is common, and useful in the real world. We proved the method's correctness.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.