How can you strip non-ASCII characters from a string? (in C#)

前端 未结 11 1086
迷失自我
迷失自我 2020-11-22 17:24

How can you strip non-ASCII characters from a string? (in C#)

11条回答
  •  遇见更好的自我
    2020-11-22 17:34

    This is not optimal performance-wise, but a pretty straight-forward Linq approach:

    string strippedString = new string(
        yourString.Where(c => c <= sbyte.MaxValue).ToArray()
        );
    

    The downside is that all the "surviving" characters are first put into an array of type char[] which is then thrown away after the string constructor no longer uses it.

提交回复
热议问题