Removing hidden characters from within strings

前端 未结 8 1266
既然无缘
既然无缘 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:14
    new string(input.Where(c => !char.IsControl(c)).ToArray());
    

    IsControl misses some control characters like left-to-right mark (LRM) (the char which commonly hides in a string while doing copy paste). If you are sure that your string has only digits and numbers then you can use IsLetterOrDigit

    new string(input.Where(c => char.IsLetterOrDigit(c)).ToArray())
    

    If your string has special characters, then

    new string(input.Where(c => c < 128).ToArray())
    
    0 讨论(0)
  • 2020-12-01 11:18
    string output = new string(input.Where(c => !char.IsControl(c)).ToArray());
    

    This will surely solve the problem. I had a non printable substitute characer(ASCII 26) in a string which was causing my app to break and this line of code removed the characters

    0 讨论(0)
提交回复
热议问题