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

前端 未结 9 917
孤城傲影
孤城傲影 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:30

    if you still want to do it in a LINQy way:

    public static string CleanUp(this string orig)
    {
        var badchars = new HashSet() { '!', '@', '#', '$', '%', '_' };
    
        return new string(orig.Where(c => !badchars.Contains(c)).ToArray());
    }
    

提交回复
热议问题