I have string array that contain characters that aren't allowed:
public static string[] IllegalCharacters = { "\"", "/", "\\", "[", "]", ":", "|", "<", ">", "+", "=", ";", ",", "?", "*", "\'", "@", ".", ":", "^", "¨", "å", "ä", "ö", "Å", "Ä", "Ö" };
I could remove the characters one after another with a foreach like this:
private string RemoveIllegalCharactersFromString(string text)
{
foreach (string illegalCharacter in IllegalCharacters.IllegalCharacters)
{
text = text.Replace(illegalCharacter, "");
}
return text;
}
My question is could I remove the method by using a linq lambda expression instead?
What I'm using now:
public static HashSet<char> IllegalCharacters = new HashSet<char>(new char[] { '\"', '/', '\\', '[', ']', ':', '|', '<', '>', '+', '=', ';', ',', '?', '*', '\'', '@', '.', ':', '^', '¨','\'' });
var t = sourceText.Where(c => !IllegalCharacters.Contains(c)).ToArray();
var result = new string(t);
return result;