C Method for Deleting Numbers or Non Numbers from Strings Based on Regular Expressions

  • 2021-12-19 06:35:44
  • OfStack

This article illustrates the C # method of deleting digits or non-digits from strings based on regular expressions. Share it for your reference, as follows:


///  Remove numbers from a string 
public static string RemoveNumber(string key)
{
  return Regex.Replace(key, @"\d", "");
}
// Remove non-digits from strings 
public static string RemoveNotNumber(string key)
{
  return Regex.Replace(key, @"[^\d]*", "");
}

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

For more readers interested in C # related content, please check the topics on this site: "C # Regular Expression Usage Summary", "C # Coding Operation Skills Summary", "XML File Operation Skills Summary in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: