开发者

How to replace String characters in C# [closed]

开发者 https://www.devze.com 2023-01-22 20:40 出处:网络
开发者_如何学JAVA It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current fo
开发者_如何学JAVA It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号