Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# replace multiple characters

You could use Linq's Aggregate function:

string s = "the
quick	brown
dog,jumped;over the lazy fox.";
char[] chars = new char[] { ' ', ';', ',', '
', '	', '
' };
string snew = chars.Aggregate(s, (c1, c2) => c1.Replace(c2, '
'));
Here's the extension method:

public static string ReplaceAll(this string seed, char[] chars, char replacementCharacter)
{
    return chars.Aggregate(seed, (str, cItem) => str.Replace(cItem, replacementCharacter));
}
Extension method usage example:

string snew = s.ReplaceAll(chars, '
');
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #replace #multiple #characters
ADD COMMENT
Topic
Name
6+4 =