Removing hidden characters from within strings

前端 未结 8 1306
既然无缘
既然无缘 2020-12-01 10:37

My problem:

I have a .NET application that sends out newsletters via email. When the newsletters are viewed in outlook, outlook displays a question mark in place

8条回答
  •  醉话见心
    2020-12-01 11:07

    What best worked for me is:

    string result = new string(value.Where(c =>  char.IsLetterOrDigit(c) || (c >= ' ' && c <= byte.MaxValue)).ToArray());
    

    Where I'm making sure the character is any letter or digit, so that I don't ignore any non English letters, or if it is not a letter I check whether it's an ascii character that is greater or equal than Space to make sure I ignore some control characters, this ensures I don't ignore punctuation.

    Some suggest using IsControl to check whether the character is non printable or not, but that ignores Left-To-Right mark for example.

提交回复
热议问题