Following code will remove all non-printable characters from a string.

 StringBuilder cleanString = new StringBuilder();
 foreach (char character in inputString)
 {
     if (Char.IsControl(character))
     {
         if (Char.IsWhiteSpace(character))
         {
             cleanString.Append(" ");
         }
         continue;
     }
     cleanString.Append(character);
 }

1 Comment


  1. For XML, you can also use the framework method XmlConvert.IsXmlChar.

Leave a Reply

Your email address will not be published.

*