skip to Main Content

I am looking to take a string from an input text box and remove all special characters and spaces.

e.g.
Input: ##HH 4444 5656 3333 2AB##
Output: HH4444565633332AB

2

Answers


  1. Let’s define what we are going to keep, not what to remove. If we keep Latin letters and digits only we can put

    string result = Regex.Replace(input, "[^A-Za-z0-9]", "");
    

    or (Linq alternative)

    string result = string.Concat(input
      .Where(c => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' c >= '0' && c <= '9'));
    
    Login or Signup to reply.
  2. If dealing with unicode to remove one or more characters that is not a letter nor a number:

    [^p{L}p{N}]+
    

    See this demo at regexstorm or a C# replace demo at tio.run

    • p{L} matches any kind of letter from any language
    • p{N} matches any kind of numeric character in any script
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search