How do I strip non-alphanumeric characters (including spaces) from a string?

前端 未结 8 2316
借酒劲吻你
借酒劲吻你 2020-12-14 00:10

How do I strip non alphanumeric characters from a string and loose spaces in C# with Replace?

I want to keep a-z, A-Z, 0-9 and nothing more (not even \" \" spaces).<

8条回答
  •  执笔经年
    2020-12-14 00:37

    In .Net 4.0 you can use the IsNullOrWhitespace method of the String class to remove the so called white space characters. Please take a look here http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx However as @CodeInChaos pointed there are plenty of characters which could be considered as letters and numbers. You can use a regular expression if you only want to find A-Za-z0-9.

提交回复
热议问题