Removing control characters from a UTF-8 string

后端 未结 3 1848
忘了有多久
忘了有多久 2021-01-11 10:03

I found this question but it removes all valid utf-8 characters also (returns me a blank string, while there are valid utf-8 characters plus contro

3条回答
  •  一整个雨季
    2021-01-11 10:52

    This is how I roll:

    Regex.Replace(evilWeirdoText, @"[\u0000-\u001F]", string.Empty)
    

    This strips out all the first 31 control characters. The next hex value up from \u001F is \u0020 AKA the space. Everything before space is all the line feed and null nonsense.

    To believe me on the characters: http://donsnotes.com/tech/charsets/ascii.html

提交回复
热议问题