开发者

Unable to remove invisible chars using Regex

I want to remove any invisible chars from a string, only keep spaces & any开发者_如何学Go chars from 0x20-0x7F, I use this: Regex.Replace(QueryString, @"[^\s\x20-\x7F]", ""); However it does not work

QueryString has a char 0xA0, after that, the char still exists in QueryString.

I am not sure why this failed to work?


0xA0 is the non-breaking space character - and as such it's matched with \s. Rather than using \s, expand this out into the list of whitespace characters you want to include.


I think you would rather use StringBuilder to process such strings.

StringBuilder sb = new StringBuilder(str.Length);
foreach(char ch in str)
{
    if (0x20 <= ch && ch <= 0x7F)
    {
        sb.Append(ch)
    }
}

string result = sb.ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜