How do I detect non-printable characters in .NET?

前端 未结 4 2078
花落未央
花落未央 2020-12-30 18:43

I\'m just wondering if there is a method in .NET 2.0 that checks whether a character is printable or not – something like isprint(int) from standard C.

4条回答
  •  清酒与你
    2020-12-30 19:25

    In addition to Char.IsControlChar() there are several other functions that can be used to determine what category a given char value is:

    • IsLetter()
    • IsNumber()
    • IsDigit()
    • IsLetterOrDigit()
    • IsSymbol()
    • IsPunctuation()
    • IsSeparator()
    • IsWhiteSpace()

    If what you have is a "traditional ASCII text" file, and you want to use supplied functions, the expression:

    (Char.IsLetterOrDigit(ch) || Char.IsPunctuation(ch) || Char.IsSymbol(ch) || (ch==' '))
    

    should work.

    Now, if you are working with Unicode, you are opening a can or worms. Even back in the day, whether a space is printable or not printable was open to interpretation (hence the isprint() and isgraph() functions). See this related question and answers about "printable" unicode characters.

提交回复
热议问题