Answers for "replace multiple characters in string c#"

C#
0

c# replace multiple characters

You could use Linq's Aggregate function:

string s = "the\nquick\tbrown\rdog,jumped;over the lazy fox.";
char[] chars = new char[] { ' ', ';', ',', '\r', '\t', '\n' };
string snew = chars.Aggregate(s, (c1, c2) => c1.Replace(c2, '\n'));
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, '\n');
Posted by: Guest on January-13-2021
0

replace multiple characters in string c#

string name="$1,300";
Console.Write((name.Replace("$","").Replace(",",""))); // output->  1300
Posted by: Guest on February-23-2021

Code answers related to "replace multiple characters in string c#"

C# Answers by Framework

Browse Popular Code Answers by Language