5

I am new to C#. Say that I have a string like this:

string test = 'yes/, I~ know# there@ are% invalid£ characters$ in& this* string^";

If I wanted to get rid of a single invalid symbol, I would do:

if (test.Contains('/')) 
{ 
    test = test.Replace("/","");
} 

But is there a way I can use a list of symbols as argument of the Contains and Replace functions, instead of deleting symbols one by one?

Zizzipupp
  • 1,301
  • 1
  • 11
  • 27
  • 1
    One option is to replace one character after another. Another option is to use regular expressions, which I do not prefer because of readability reasons. – user743414 Dec 05 '19 at 12:57
  • I think it's `char[]`. You need to use `char[] x = {'/'};` Then `test.Replace(x, "")`; Or `test.Replace('/', \"");` – Mr Hery Dec 05 '19 at 13:00
  • https://stackoverflow.com/a/7265336/12258072 and https://stackoverflow.com/a/7411472/12258072 – Longoon12000 Dec 05 '19 at 13:00
  • 1
    Does this answer your question? [Replace multiple characters in a C# string](https://stackoverflow.com/questions/7265315/replace-multiple-characters-in-a-c-sharp-string) – Glenn van Acker Dec 05 '19 at 13:01
  • `string result = Regex.Replace(test, @"[^\p{L}\d\s,.;:!?\-]", "");` - we spare letters, digits, white spaces and some punctuations – Dmitry Bychenko Dec 05 '19 at 13:10

5 Answers5

4

You'll likely be better off defining acceptable characters than trying to think of and code for everything you need to eliminate.

Because you mention that you are learning, sounds like the perfect time to learn about Regular Expressions. Here are a couple of links to get you started:

Roger Oney
  • 41
  • 1
4

I would go with the regular expression solution

string test = Regex.Replace(test, @"\/|~|#|@|%|£|\$|&|\*|\^", "");

Add a | or parameter for each character and use the replace

Bear in mind the \/ means / but you need to escape the character.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
3

I don't think there is such a feature out of the box.

I think your idea is pretty much on point, despite the fact the in my opinion you don't really need the if(test.Contains(..)) part. Doing this, once you iterate the characters of the string to see if such element is present when at the end if indeed this character is in the string you replace it

It would be faster just to replace the special characters right away. So...

List<string> specialChars = new List<string>() {"*", "/", "&"}

for (var i = 0; i < specialChars.Count; i++) 
{
  test = test.Replace(specialChars[i],"");
}
Leron
  • 9,546
  • 35
  • 156
  • 257
2

Your solution is:

Path.GetInvalidPathChars()

So the code would look something like this:

string illegal = "yes/, I~ know# there@ are% invalid£ characters$ in& this* string^";
string invalid = new string(Path.GetInvalidFileNameChars()) + new 
string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
    illegal = illegal.Replace(c.ToString(), "");    
}
ExtremAdn
  • 31
  • 4
  • Good solution, except that you´re referring to invalid characters for a path-name, which might not be what OP actually wants. E.g. `#` is okay for a path. – MakePeaceGreatAgain Dec 05 '19 at 13:06
2

Another variant:

List<string> chars = new List<string> {"!", "@"};
string test  = "My funny! string@";
foreach (var c in chars)
{
    test = test.Replace(c,"");  
}

No need to use Contains as Replace does that.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77