0

How to replace different/multiple chars with white space for each instance of character?

characters to be replaced are \ / : * ? < > |

tbreffni
  • 5,082
  • 5
  • 31
  • 30
Ahmed Atia
  • 17,848
  • 25
  • 91
  • 133

6 Answers6

8

You can achieve this with string.Split and string.Join:

string myString = string.Join(" ", input.Split(@"\/:*?<>|".ToCharArray())); 

Out of curiousity tested this for performance, and it is considerably faster than the Regex approach.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • +1 for originality and an approach I started out with but didn't conclude (and for showing me `string.Join` instead of `Split(..).Join()`) :) – Abel Oct 27 '09 at 18:13
  • the funny thing about this answer is that depending on which you are more familiar with, this could be more/less readable than RegEx. But for me, RegEx is more readable. – Aaron Hoffman Oct 27 '09 at 18:15
  • According to Jon Skeet's answer http://stackoverflow.com/a/1280227/52277 performance depends on input format – Michael Freidgeim Jul 04 '13 at 11:42
3
Regex.Replace(@"my \ special / : string", @"[\\/:*?<>|]", " ");

I might have some of the escapes wrong... :/

JustLoren
  • 3,224
  • 3
  • 27
  • 34
  • Unfortunately, they are wrong and it won't compile. I think you mean this: `Regex.Replace(@"my \ ?? special / : string", @"[\\/:\*?<>|]", " ");` (note that `\?` etc is allowed, but not necessary in a character class) – Abel Oct 27 '09 at 18:08
1

System.Text.RegularExpressions.Regex.Replace(input, @"[\\/:*?<>|]", " ")

Joren
  • 14,472
  • 3
  • 50
  • 54
  • actually I think you mean. @"[\\/:*\?\<\>\|]", because many of those characters are Reg Ex control characters. – Nick Berardi Oct 27 '09 at 18:00
  • Regex control characters are not regex control characters inside a character class, with the exception of `-`, `[` and `\\`. – Abel Oct 27 '09 at 18:05
  • You're right, I should've thought about character escapes a bit more. I've edited to something I do think is correct. – Joren Oct 27 '09 at 18:47
0

Look at the String API methods in C#.

String.replace would work, if you called it seven times.
Or String.indexOfAny in a loop, using String.remove and String.insert.

Going the efficient lines of code way, Regexp.

Dean J
  • 39,360
  • 16
  • 67
  • 93
0

Here's a compilable piece of code:

// input
string input = @"my \ ?? spe<<||>>cial / : string";

// regex
string test = Regex.Replace(input, @"[\\/:*?<>|]", " ");

// test now contains "my      spe      cial     string"

Note: this post is a fix of the original JustLoren's code, it's not entirely mine.

Abel
  • 56,041
  • 24
  • 146
  • 247
0

You can do it using Regex

static void Main(string[] args)       
{        
    string myStr = @"\ / : * ? < > |";
    Regex myRegex = new Regex(@"\\|\/|\:|\*|\?|\<|\>|\|");
    string replaced = myRegex.Replace(myStr, new MatchEvaluator(OnMatch));
    Console.WriteLine(replaced);    
}

private static string OnMatch(Match match)
{
    return " ";
}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • when posting code, indent it with four spaces (or click the Code button) to get it correctly colored and laid out. – Abel Oct 27 '09 at 18:16