How I set new value for an string by index value?
I tried:
string a = "abc";
a[0] = "A";
not works for strings, but yes for chars. Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Strings in C# (and other .NET languages which use
System.Stringin the base class library) are immutable. That is, you can’t modify a string character by character that way (or for that matter, can you modify a string ever).If you want to modify a string based on the index, you have to convert it to an array using
System.String.ToCharArray()first. You convert it back to a string usingSystem.String‘s constructor, passing in the modified array.Your example would have to be changed to look like: