2

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;
George Norberg
  • 217
  • 2
  • 15
  • 2
    You should test if your approach isn't more efficient, you could improve it easily by using `StringBuilder.Replace` in a loop like [here](http://stackoverflow.com/a/25154888/284240). – Tim Schmelter May 24 '16 at 08:42
  • Could you provide any info why using a StringBuilder would be "faster" – George Norberg May 24 '16 at 08:49
  • 1
    i haven't said "faster" but "more efficient". It might use less memory or prevent the garbage collector from kicking in. So `StringBuilder.Replace` [can be more efficient](http://stackoverflow.com/a/6524918/284240).Btw, if you want to remove substrings instead of chars the LINQ approaches won't work anymore. – Tim Schmelter May 24 '16 at 08:50

3 Answers3

4

I suggest changing IllegalCharacters into char collection (illegal characters should be characters, right?), e.g.

  public static HashSet<char> IllegalCharacters = new HashSet<char>(
    new char[] { '"', '\\', ..., 'Ä', 'Ö' });

And so you can put an evident Linq:

  string source = "123:+-456";

  string result = String.Concat(source
   .Where(c => !IllegalCharacters.Contains(c)));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
3

Here is a approach with Linq

string Text = "1:2+3[4]5|6";
string Result = new string(Text.Where(x => !IllegalCharacters.Contains(x.ToString())).ToArray());

I would store the characters in type char instead of string and use a HashSet instead of string[]:

HashSet<char> IllegalCharacters = new HashSet<char>() { '\'', '/', '\\', '[', ']', ':', '|', '<', '>', '+', '=', ';', ',', '?', '*', '\'', '@', '.', ':', '^', '¨', 'å', 'ä', 'ö', 'Å', 'Ä', 'Ö' };

string Text = "1:2+3[4]5|6";
string Result = new string(Text.Where(x => !IllegalCharacters.Contains(x)).ToArray());
fubo
  • 44,811
  • 17
  • 103
  • 137
2

You could use Aggregate. Like this:

private string RemoveIllegalCharactersFromString(string text)
{
    return IllegalCharacters.Aggregate(text, (current, illegalCharacter) => current.Replace(illegalCharacter, ""));
}

The result:

string result = RemoveIllegalCharactersFromString("HelloÄ[2536]:Y?eÖs*");
//Result => Hello2536Yes
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109