Regex 101 Discussion I4 – remove unprintable characters from a string


Exercise I4 – remove unprintable characters from a string


Given an input string, remove all characters that are not printable.


————–


Assuming ASCII – or something like ASCII – non-printing characters have ascii values of 31 or lower. You can match them with the following:


           [\x01-\x1F]


and then user Regex.Replace() to replace each of those characters with an empty string.

Comments (5)

  1. orcmid says:

    Oops, you forgot about DEL (0x7F) and technically there can be problems at 0x80 and above depending on the 8-bit code being used.

    In the case of ASCII carried in 8-bit bytes, the 0x80-0xFF codes are technically illegal. Would you declare those to be non-printing too?

  2. Maurits says:

    Does .Net allow embedded NULLs in strings?

  3. Ken says:

    Maurits,

    No, .NET doesn’t allow embedded NULLs… However, that’s not what Eric suggested. He said empty spaces, which is not a null; it’s an empty string.