2

Just found this little nugget... So much wrong with this.

public static class CleanString
{
public static string CleanString(this String str)
{
if (str != "" && str != null)
{
str = str.Replace("!", "");
str = str.Replace("£", "");
str = str.Replace("$", "");
str = str.Replace("%", "");
str = str.Replace("^", "");
str = str.Replace("&", "");
str = str.Replace("*", "");
str = str.Replace("(", "");
str = str.Replace(")", "");
str = str.Replace("-", "");
str = str.Replace("=", "");
str = str.Replace("{", "");
str = str.Replace("}", "");
str = str.Replace("[", "");
str = str.Replace("]", "");
.....
return str;
}
}
}

Comments
  • 1
    "public static string CleanString(this String str)"

    Why the redundant "String" in the extension name?

    Why use the class name and not the alias IE string not String.

    "if (str != "" && str != null)"

    string.IsNullOrEmpty() - also, its an extension method so str will never be null.

    "str = str.Replace("!", "");" Well, there are many different implementations, and this has to be the absolute worse.
  • 0
    @crapped tbf I rewrote this pretty fast as I couldn’t be arsed to go find it in source control. I think the original did return null in the event the if statement evaluated to false.
Add Comment