Removing control characters from a UTF-8 string

后端 未结 3 1808
忘了有多久
忘了有多久 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:40

    I think the following code will work for you:

    public static string RemoveControlCharacters(string inString)
    {
        if (inString == null) return null;
        StringBuilder newString = new StringBuilder();
        char ch;
        for (int i = 0; i < inString.Length; i++)
        {
            ch = inString[i];
            if (!char.IsControl(ch))
            {
                newString.Append(ch);
            }
        }
        return newString.ToString();
    }
    

提交回复
热议问题