How to replace String characters in C# [closed]
I would like to replace specific String characters based on some integers value passed dynamically to its index.
Unfortunately, String.Replace() expects String value as its arguments. So could anyone please tell me how can I make my requirement possible?
Strings are immutable; to change an individual character you might use:
char[] chars = oldString.ToCharArray();
chars[index] = newChar;
string newString = new string(chars);
It's usually better with changing strings to use a StringBuilder. It has a StringBuilder.Replace method that can replace characters.
If you are going to replace lot of words within the string then it is always better to use stringBuilder. String is slower compared to StringBuilder. To come over limitations in String datatype, stringbuilder was introduced.
精彩评论