Reputation:
how to I replace all characters and numbers in string except several chars, for example "f", "a", "l" to avoid somthing like this:
String str = "replace different characters except several";
Console.WriteLine("Input: " + str);
str = str.Replace('a', '.').Replace('b', '.').Replace('c', '.');
Console.WriteLine("Output: " + str);
Upvotes: 2
Views: 681
Reputation: 16956
One way would be using Linq
and replacing every character except those in excluded list.
char[] excluded = new char[] {'f', 'a', 'l'};
var output = new string(str.Select(x=> excluded.Contains(x)? x:'.').ToArray());
Check this demo
Upvotes: 0