C#: Removing common invalid characters from a string: improve this algorithm

前端 未结 9 919
孤城傲影
孤城傲影 2020-12-29 07:18

Consider the requirement to strip invalid characters from a string. The characters just need to be removed and replace with blank or string.Empty.



        
9条回答
  •  借酒劲吻你
    2020-12-29 07:32

    This is pretty clean. Restricts it to valid characters instead of removing invalid ones. You should split it to constants probably:

    string clean = new string(@"Sour!ce Str&*(@ing".Where(c => 
    @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ,.".Contains(c)).ToArray()
    

提交回复
热议问题