Replace Multiple Characters in a C# String

Replace multiple characters in a C# string

You can use a replace regular expression.

s/[;,\t\r ]|[\n]{2}/\n/g
  • s/ at the beginning means a search
  • The characters between [ and ] are the characters to search for (in any order)
  • The second / delimits the search-for text and the replace text

In English, this reads:

"Search for ; or , or \t or \r or (space) or exactly two sequential \n and replace it with \n"

In C#, you could do the following: (after importing System.Text.RegularExpressions)

Regex pattern = new Regex("[;,\t\r ]|[\n]{2}");
pattern.Replace(myString, "\n");

Replacing multiple characters in a string in c# by a one liner

You can use Regex.Replace:

string output = Regex.Replace(input, "[$|&]", " ");

Replace Multiple Characters in string

You cannot use Replace because it works its replacement operation on the whole string, not char by char.

A simple brute force solution could be this one

void Main()
{
// A dictionary where every key points to its replacement char
Dictionary<char, char> replacements = new Dictionary<char, char>()
{
{'(', ')'},
{')', '('},
};

string source = ")Hi(";

StringBuilder sb = new StringBuilder();
foreach (char c in source)
{
char replacement = c;
if(replacements.ContainsKey(c))
replacement = replacements[c];
sb.Append(replacement,1);

}
Console.WriteLine(sb.ToString());
}

You can transform this in an extension method adding to a static class

public static class StringExtensions
{
public static string ProgressiveReplace(this string source, Dictionary<char, char> replacements)
{
StringBuilder sb = new StringBuilder();
foreach (char c in source)
{
char replacement = c;
if (replacements.ContainsKey(c))
replacement = replacements[c];
sb.Append(replacement, 1);
}
return sb.ToString();
}
}

and call it from your code with

Dictionary<char, char> replacements = new Dictionary<char, char>() 
{{'(', ')'},{')', '('}};
s = s.ProgressiveReplace(replacements);

Replace multiple characters in a string with different characters

Since you mentioned in comment that the data originally came from dictionary, you could loop through the dictionary and format the data as you like.

Sample code with Dictionary<string, object>:

var dict = new Dictionary<string, object>();
dict.Add("key1", 1);
dict.Add("key2", "string");
dict.Add("key3", true);
dict.Add("key4", new DateTime(2020, 1, 1));

foreach (KeyValuePair<string, object> kv in dict) {
Console.WriteLine(string.Format("{0}={1}", kv.Key, kv.Value.ToString()));
}

Replace Multiple String Elements in C#

Quicker - no. More effective - yes, if you will use the StringBuilder class. With your implementation each operation generates a copy of a string which under circumstances may impair performance. Strings are immutable objects so each operation just returns a modified copy.

If you expect this method to be actively called on multiple Strings of significant length, it might be better to "migrate" its implementation onto the StringBuilder class. With it any modification is performed directly on that instance, so you spare unnecessary copy operations.

public static class StringExtention
{
public static string clean(this string s)
{
StringBuilder sb = new StringBuilder (s);

sb.Replace("&", "and");
sb.Replace(",", "");
sb.Replace(" ", " ");
sb.Replace(" ", "-");
sb.Replace("'", "");
sb.Replace(".", "");
sb.Replace("eacute;", "é");

return sb.ToString().ToLower();
}
}

Replace multiple characters in C# using .replace without creating a loop

Before going to the answer let me describe what went wrong in your case, Actually the replace operations returns a new instance of the string so after the first replace(t1.Replace('A','B') the resulting string becomes BBCD(A is replaced with B) and you are performing the next replace operation in this string, hence every B will be replaced with C. so before final Replace your input string becomes DDDD.

I've a simple solution using LINQ with String.Join, You can take a look into the working example here

string inputString = "ABCD";
var ReplacedString = String.Join("", inputString.Select(x => x == 'A' ? 'B' :
x == 'B' ? 'C' :
x == 'C' ? 'D' :
x == 'D' ? 'E' :
x));

Replacing multiple characters in a string, the fastest way?

Thanks for your inputs guys.
I wrote a quick and dirty benchmark to test your inputs. I have tested parsing 4 strings with 500.000 iterations and have done 4 passes. The result is as follows:


*** Pass 1
Old (Chained String.Replace()) way completed in 814 ms
logicnp (ToCharArray) way completed in 916 ms
oleksii (StringBuilder) way completed in 943 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 2551 ms
Richard (Regex w/ MatchEvaluator) way completed in 215 ms
Marc Gravell (Static Regex) way completed in 1008 ms

*** Pass 2
Old (Chained String.Replace()) way completed in 786 ms
logicnp (ToCharArray) way completed in 920 ms
oleksii (StringBuilder) way completed in 905 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 2515 ms
Richard (Regex w/ MatchEvaluator) way completed in 217 ms
Marc Gravell (Static Regex) way completed in 1025 ms

*** Pass 3
Old (Chained String.Replace()) way completed in 775 ms
logicnp (ToCharArray) way completed in 903 ms
oleksii (StringBuilder) way completed in 931 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 2529 ms
Richard (Regex w/ MatchEvaluator) way completed in 214 ms
Marc Gravell (Static Regex) way completed in 1022 ms

*** Pass 4
Old (Chained String.Replace()) way completed in 799 ms
logicnp (ToCharArray) way completed in 908 ms
oleksii (StringBuilder) way completed in 938 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 2592 ms
Richard (Regex w/ MatchEvaluator) way completed in 225 ms
Marc Gravell (Static Regex) way completed in 1050 ms

The code for this benchmark is below. Please review the code and confirm that @Richard has got the fastest way. Note that I haven't checked if outputs were correct, I assumed they were.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace StringReplaceTest
{
class Program
{
static string test1 = "A^@[BCD";
static string test2 = "E]FGH\\";
static string test3 = "ijk`l}m";
static string test4 = "nopq~{r";

static readonly Dictionary<char, string> repl =
new Dictionary<char, string>
{
{'^', "Č"}, {'@', "Ž"}, {'[', "Š"}, {']', "Ć"}, {'`', "ž"}, {'}', "ć"}, {'~', "č"}, {'{', "š"}, {'\\', "Đ"}
};

static readonly Regex replaceRegex;

static Program() // static initializer
{
StringBuilder pattern = new StringBuilder().Append('[');
foreach (var key in repl.Keys)
pattern.Append(Regex.Escape(key.ToString()));
pattern.Append(']');
replaceRegex = new Regex(pattern.ToString(), RegexOptions.Compiled);
}

public static string Sanitize(string input)
{
return replaceRegex.Replace(input, match =>
{
return repl[match.Value[0]];
});
}

static string DoGeneralReplace(string input)
{
var sb = new StringBuilder(input);
return sb.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ').ToString();
}

//Method for replacing chars with a mapping
static string Replace(string input, IDictionary<char, char> replacementMap)
{
return replacementMap.Keys
.Aggregate(input, (current, oldChar)
=> current.Replace(oldChar, replacementMap[oldChar]));
}

static void Main(string[] args)
{
for (int i = 1; i < 5; i++)
DoIt(i);
}

static void DoIt(int n)
{
Stopwatch sw = new Stopwatch();
int idx = 0;

Console.WriteLine("*** Pass " + n.ToString());
// old way
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = test1.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
string result2 = test2.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
string result3 = test3.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
string result4 = test4.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
}
sw.Stop();
Console.WriteLine("Old (Chained String.Replace()) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

Dictionary<char, char> replacements = new Dictionary<char, char>();
replacements.Add('^', 'Č');
replacements.Add('@', 'Ž');
replacements.Add('[', 'Š');
replacements.Add(']', 'Ć');
replacements.Add('`', 'ž');
replacements.Add('}', 'ć');
replacements.Add('~', 'č');
replacements.Add('{', 'š');
replacements.Add('\\', 'Đ');

// logicnp way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
char[] charArray1 = test1.ToCharArray();
for (int i = 0; i < charArray1.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test1[i], out newChar))
charArray1[i] = newChar;
}
string result1 = new string(charArray1);

char[] charArray2 = test2.ToCharArray();
for (int i = 0; i < charArray2.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test2[i], out newChar))
charArray2[i] = newChar;
}
string result2 = new string(charArray2);

char[] charArray3 = test3.ToCharArray();
for (int i = 0; i < charArray3.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test3[i], out newChar))
charArray3[i] = newChar;
}
string result3 = new string(charArray3);

char[] charArray4 = test4.ToCharArray();
for (int i = 0; i < charArray4.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test4[i], out newChar))
charArray4[i] = newChar;
}
string result4 = new string(charArray4);
}
sw.Stop();
Console.WriteLine("logicnp (ToCharArray) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// oleksii way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = DoGeneralReplace(test1);
string result2 = DoGeneralReplace(test2);
string result3 = DoGeneralReplace(test3);
string result4 = DoGeneralReplace(test4);
}
sw.Stop();
Console.WriteLine("oleksii (StringBuilder) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// André Christoffer Andersen way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = Replace(test1, replacements);
string result2 = Replace(test2, replacements);
string result3 = Replace(test3, replacements);
string result4 = Replace(test4, replacements);
}
sw.Stop();
Console.WriteLine("André Christoffer Andersen (Lambda w/ Aggregate) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// Richard way
sw.Reset();
sw.Start();
Regex reg = new Regex(@"\^|@|\[|\]|`|\}|~|\{|\\");
MatchEvaluator eval = match =>
{
switch (match.Value)
{
case "^": return "Č";
case "@": return "Ž";
case "[": return "Š";
case "]": return "Ć";
case "`": return "ž";
case "}": return "ć";
case "~": return "č";
case "{": return "š";
case "\\": return "Đ";
default: throw new Exception("Unexpected match!");
}
};
for (idx = 0; idx < 500000; idx++)
{
string result1 = reg.Replace(test1, eval);
string result2 = reg.Replace(test2, eval);
string result3 = reg.Replace(test3, eval);
string result4 = reg.Replace(test4, eval);
}
sw.Stop();
Console.WriteLine("Richard (Regex w/ MatchEvaluator) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// Marc Gravell way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = Sanitize(test1);
string result2 = Sanitize(test2);
string result3 = Sanitize(test3);
string result4 = Sanitize(test4);
}
sw.Stop();
Console.WriteLine("Marc Gravell (Static Regex) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms\n");
}
}
}

EDIT June 2020
Since this Q&A is still getting hits, I wanted to update it with additional input from user1664043 using StringBuilder w/ IndexOfAny, this time compiled using .NET Core 3.1, and here are the results:


*** Pass 1
Old (Chained String.Replace()) way completed in 199 ms
logicnp (ToCharArray) way completed in 296 ms
oleksii (StringBuilder) way completed in 416 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 870 ms
Richard (Regex w/ MatchEvaluator) way completed in 1722 ms
Marc Gravell (Static Regex) way completed in 395 ms
user1664043 (StringBuilder w/ IndexOfAny) way completed in 459 ms

*** Pass 2
Old (Chained String.Replace()) way completed in 215 ms
logicnp (ToCharArray) way completed in 239 ms
oleksii (StringBuilder) way completed in 341 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 758 ms
Richard (Regex w/ MatchEvaluator) way completed in 1591 ms
Marc Gravell (Static Regex) way completed in 354 ms
user1664043 (StringBuilder w/ IndexOfAny) way completed in 426 ms

*** Pass 3
Old (Chained String.Replace()) way completed in 199 ms
logicnp (ToCharArray) way completed in 265 ms
oleksii (StringBuilder) way completed in 337 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 817 ms
Richard (Regex w/ MatchEvaluator) way completed in 1666 ms
Marc Gravell (Static Regex) way completed in 373 ms
user1664043 (StringBuilder w/ IndexOfAny) way completed in 412 ms

*** Pass 4
Old (Chained String.Replace()) way completed in 199 ms
logicnp (ToCharArray) way completed in 230 ms
oleksii (StringBuilder) way completed in 324 ms
André Christoffer Andersen (Lambda w/ Aggregate) way completed in 791 ms
Richard (Regex w/ MatchEvaluator) way completed in 1699 ms
Marc Gravell (Static Regex) way completed in 359 ms
user1664043 (StringBuilder w/ IndexOfAny) way completed in 413 ms

And the updated code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Test.StringReplace
{
class Program
{
static string test1 = "A^@[BCD";
static string test2 = "E]FGH\\";
static string test3 = "ijk`l}m";
static string test4 = "nopq~{r";

static readonly Dictionary<char, string> repl =
new Dictionary<char, string>
{
{'^', "Č"}, {'@', "Ž"}, {'[', "Š"}, {']', "Ć"}, {'`', "ž"}, {'}', "ć"}, {'~', "č"}, {'{', "š"}, {'\\', "Đ"}
};

static readonly Regex replaceRegex;

static readonly char[] badChars = new char[] { '^', '@', '[', ']', '`', '}', '~', '{', '\\' };

static readonly char[] replacementChars = new char[] { 'Č', 'Ž', 'Š', 'Ć', 'ž', 'ć', 'č', 'š', 'Đ' };

static Program() // static initializer
{
StringBuilder pattern = new StringBuilder().Append('[');
foreach (var key in repl.Keys)
pattern.Append(Regex.Escape(key.ToString()));
pattern.Append(']');
replaceRegex = new Regex(pattern.ToString(), RegexOptions.Compiled);
}

public static string Sanitize(string input)
{
return replaceRegex.Replace(input, match =>
{
return repl[match.Value[0]];
});
}

static string DoGeneralReplace(string input)
{
var sb = new StringBuilder(input);
return sb.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ').ToString();
}

//Method for replacing chars with a mapping
static string Replace(string input, IDictionary<char, char> replacementMap)
{
return replacementMap.Keys
.Aggregate(input, (current, oldChar)
=> current.Replace(oldChar, replacementMap[oldChar]));
}

static string ReplaceCharsWithIndexOfAny(string sIn)
{
int replChar = sIn.IndexOfAny(badChars);
if (replChar < 0)
return sIn;

// Don't even bother making a copy unless you know you have something to swap
StringBuilder sb = new StringBuilder(sIn, 0, replChar, sIn.Length + 10);
while (replChar >= 0 && replChar < sIn.Length)
{
var c = replacementChars[replChar];
sb.Append(c);

////// This approach lets you swap a char for a string or to remove some
////// If you had a straight char for char swap, you could just have your repl chars in an array with the same ordinals and do it all in 2 lines matching the ordinals.
////c = c switch
////{
//// ////case "^":
//// //// c = "Č";
//// //// ...
//// '\ufeff' => null,
//// _ => replacementChars[replChar],
////};

////if (c != null)
////{
//// sb.Append(c);
////}

replChar++; // skip over what we just replaced
if (replChar < sIn.Length)
{
int nextRepChar = sIn.IndexOfAny(badChars, replChar);
sb.Append(sIn, replChar, (nextRepChar > 0 ? nextRepChar : sIn.Length) - replChar);
replChar = nextRepChar;
}
}

return sb.ToString();
}

static void Main(string[] args)
{
for (int i = 1; i < 5; i++)
DoIt(i);
}

static void DoIt(int n)
{
Stopwatch sw = new Stopwatch();
int idx = 0;

Console.WriteLine("*** Pass " + n.ToString());
// old way
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = test1.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
string result2 = test2.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
string result3 = test3.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
string result4 = test4.Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š').Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć').Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
}

sw.Stop();
Console.WriteLine("Old (Chained String.Replace()) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

Dictionary<char, char> replacements = new Dictionary<char, char>();
replacements.Add('^', 'Č');
replacements.Add('@', 'Ž');
replacements.Add('[', 'Š');
replacements.Add(']', 'Ć');
replacements.Add('`', 'ž');
replacements.Add('}', 'ć');
replacements.Add('~', 'č');
replacements.Add('{', 'š');
replacements.Add('\\', 'Đ');

// logicnp way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
char[] charArray1 = test1.ToCharArray();
for (int i = 0; i < charArray1.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test1[i], out newChar))
charArray1[i] = newChar;
}

string result1 = new string(charArray1);

char[] charArray2 = test2.ToCharArray();
for (int i = 0; i < charArray2.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test2[i], out newChar))
charArray2[i] = newChar;
}

string result2 = new string(charArray2);

char[] charArray3 = test3.ToCharArray();
for (int i = 0; i < charArray3.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test3[i], out newChar))
charArray3[i] = newChar;
}

string result3 = new string(charArray3);

char[] charArray4 = test4.ToCharArray();
for (int i = 0; i < charArray4.Length; i++)
{
char newChar;
if (replacements.TryGetValue(test4[i], out newChar))
charArray4[i] = newChar;
}

string result4 = new string(charArray4);
}

sw.Stop();
Console.WriteLine("logicnp (ToCharArray) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// oleksii way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = DoGeneralReplace(test1);
string result2 = DoGeneralReplace(test2);
string result3 = DoGeneralReplace(test3);
string result4 = DoGeneralReplace(test4);
}

sw.Stop();
Console.WriteLine("oleksii (StringBuilder) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// André Christoffer Andersen way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = Replace(test1, replacements);
string result2 = Replace(test2, replacements);
string result3 = Replace(test3, replacements);
string result4 = Replace(test4, replacements);
}

sw.Stop();
Console.WriteLine("André Christoffer Andersen (Lambda w/ Aggregate) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// Richard way
sw.Reset();
sw.Start();
Regex reg = new Regex(@"\^|@|\[|\]|`|\}|~|\{|\\");
MatchEvaluator eval = match =>
{
switch (match.Value)
{
case "^": return "Č";
case "@": return "Ž";
case "[": return "Š";
case "]": return "Ć";
case "`": return "ž";
case "}": return "ć";
case "~": return "č";
case "{": return "š";
case "\\": return "Đ";
default: throw new Exception("Unexpected match!");
}
};
for (idx = 0; idx < 500000; idx++)
{
string result1 = reg.Replace(test1, eval);
string result2 = reg.Replace(test2, eval);
string result3 = reg.Replace(test3, eval);
string result4 = reg.Replace(test4, eval);
}

sw.Stop();
Console.WriteLine("Richard (Regex w/ MatchEvaluator) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// Marc Gravell way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = Sanitize(test1);
string result2 = Sanitize(test2);
string result3 = Sanitize(test3);
string result4 = Sanitize(test4);
}

sw.Stop();
Console.WriteLine("Marc Gravell (Static Regex) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms");

// user1664043 way
sw.Reset();
sw.Start();
for (idx = 0; idx < 500000; idx++)
{
string result1 = ReplaceCharsWithIndexOfAny(test1);
string result2 = ReplaceCharsWithIndexOfAny(test2);
string result3 = ReplaceCharsWithIndexOfAny(test3);
string result4 = ReplaceCharsWithIndexOfAny(test4);
}

sw.Stop();
Console.WriteLine("user1664043 (StringBuilder w/ IndexOfAny) way completed in " + sw.ElapsedMilliseconds.ToString() + " ms\n");
}
}
}

Replacing Multiple Chars to Multiple values in a String in C#

application only takes the last Replace method (Z to G)

This is because strings are immutable. More importantly, your algorithm is broken, because subsequent substitutions "see" the results of the previous ones. You cannot do the replacements sequentially - if you want to get correct results, you must consider them all at once. Otherwise, l will become o, even though your algorithm replaces l with n:

// Let's say the text is "Hello"
text = text.replace('l', 'n'); // The text becomes "Henno"
... // More substitutions
text = text.replace('n', 'o'); // The text becomes "Heooo"

Here is how you can fix it:

StringBuilder res = new StringBuilder();
foreach (char c in txt1.Text) {
char toAppend;
switch (c) {
case 'I': toAppend = 'L'; break;
case 'j': toAppend = 'n'; break;
case 'J': toAppend = 'N'; break;
case 'k': toAppend = 'l'; break;
...
default: toAppend = '?';
}
res.append(toAppend);
}

How to replace one character with multiple characters c#

One possible approach is to write a for loop and check if the character is a * an keep track using a counter how many replacements have already been done.

Make use of the modulo % to see which character you need for the replacement and use a StringBuilder to add the characters.

For example

string s="*****\n***\n****";
StringBuilder sb = new StringBuilder();
string subj = "asd";
int counter = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] == '*') {
sb.Append(subj[counter % subj.Length]);
counter++;
continue;
}
sb.Append(s[i]);
}
Console.WriteLine(sb.ToString());

Result

asdas
das
dasd

C# demo



Related Topics



Leave a reply



Submit