Answers for "c# string multiple replace"

C#
0

c# replace multiple characters

You could use Linq's Aggregate function:

string s = "thenquicktbrownrdog,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

c# string replace multiple matches with one charactar

public static class ExtensionMethods
{
   public static string Replace(this string s, char[] separators, string newVal)
   {
       string[] temp;

       temp = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);
       return String.Join( newVal, temp );
   }
}
// use 
char[] separators = new char[]{' ',';',',','r','t','n'};
string s = "this;is,ratnnntest";

s = s.Replace(separators, "n");
Posted by: Guest on June-23-2021

Code answers related to "c# string multiple replace"

C# Answers by Framework

Browse Popular Code Answers by Language