Share via


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

  • Anonymous
    January 23, 2006
    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?

  • Anonymous
    January 23, 2006
    Does .Net allow embedded NULLs in strings?

  • Anonymous
    January 27, 2006
    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.

  • Anonymous
    June 08, 2009
    PingBack from http://quickdietsite.info/story.php?id=4518

  • Anonymous
    June 16, 2009
    PingBack from http://topalternativedating.info/story.php?id=2502