C#: get rid of multiple invalid characters in string [duplicate]
Asked Answered
P

5

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?

Petes answered 5/12, 2019 at 12:54 Comment(5)
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.Hamish
I think it's char[]. You need to use char[] x = {'/'}; Then test.Replace(x, ""); Or test.Replace('/', \"");Bioplasm
https://mcmap.net/q/117680/-replace-multiple-characters-in-a-c-string and https://mcmap.net/q/11560/-remove-characters-from-c-stringHaywire
Does this answer your question? Replace multiple characters in a C# stringHaemolysis
string result = Regex.Replace(test, @"[^\p{L}\d\s,.;:!?\-]", ""); - we spare letters, digits, white spaces and some punctuationsLiverwurst
U
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.

Unamerican answered 5/12, 2019 at 13:3 Comment(2)
One-liners are attractive. What would your solution look like in the case of the /~#@%£$&*^ symbols exactly? I am confused by the syntax.Petes
Check the edited answer.Unamerican
R
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:

Rowel answered 5/12, 2019 at 13:3 Comment(0)
U
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.

Unamerican answered 5/12, 2019 at 13:3 Comment(2)
One-liners are attractive. What would your solution look like in the case of the /~#@%£$&*^ symbols exactly? I am confused by the syntax.Petes
Check the edited answer.Unamerican
O
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],"");
}
Orlan answered 5/12, 2019 at 13:3 Comment(0)
C
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(), "");    
}
Carpophore answered 5/12, 2019 at 13:5 Comment(1)
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.Savick
S
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.

Sopor answered 5/12, 2019 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.