4

I have a string like:

string s = "abc";

And I want to change the second character to a 'd' so it will be "adc" I thought this would work:

s[1] = 'd';

However I get an error:

Property or indexer 'string.this[int] 'cannot be assigned to -- it is read only

How can I accomplish this easy task (without using reasonably complicated substring methods?

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • 2
    The reason you can't just change the one character, and instead need more complex methods, is because Strings are immutable - there is no way to modify an instance of one, and instead all of their helper methods return new, slightly-modified strings. This at least saves you on the potential confusion of figuring out why one instance of a string changed. – Katana314 Mar 23 '14 at 13:50
  • 3
    possible duplicate of [how do I set a character at an index in a string in c#?](http://stackoverflow.com/questions/3306568/how-do-i-set-a-character-at-an-index-in-a-string-in-c) and another one [Replacing a char at a given index in string?](http://stackoverflow.com/questions/9367119/replacing-a-char-at-a-given-index-in-string) – Damith Mar 23 '14 at 13:51
  • 1
    Have you checked out: http://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx –  Mar 23 '14 at 13:51
  • @Katana314 ... yes I already expected that reason, but I have a string returned from a base class and in rare occasions I have to change one character. – Michel Keijzers Mar 23 '14 at 13:52

5 Answers5

12

String is imutable. You can use StringBuilder to mutate a string:

var s = "abc";
var sb = new StringBuilder(s);
sb[1] = 'd';
s = sb.ToString();
Eli Arbel
  • 22,391
  • 3
  • 45
  • 71
2

Error message is clear, String.Chars property is a read-only. You can't assing it.

Here how it's defined;

public char this[
    int index
] { get; }

As you can see, there is no set accessor.

You can use String.Remove and String.Insert methods for this. Here an example on LINQPad;

string s = "abc";
s = s.Remove(1, 1).Insert(1, "d");
s.Dump(); //adc

Remember, string is immutable type. You can't change them. It can't be modified after it is created. Even if you think you change them, you actually create new strings object.

As an alternative, you can use StringBuilder class which represents mutable strings since StringBuilder.Chars property has get and set accessors.

var sb = new StringBuilder("abc");
sb[1] = 'd';
sb.Dump();  // adc
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2

It is not recommended, but you can do that using unsafe context:

fixed (char* pfixed = s)
    pfixed[1] = 'd';
Dmitry
  • 13,797
  • 6
  • 32
  • 48
1

Strings are immutable. You can't change a string, you can only create a new one. To modify a string like that you should use a StringBuilder.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
1

You can use Enumerable.Select:

string s = "abc";
string n = new string(s.Select((c, index) => 
            {
                if (index == 1)
                    return 'd';
                else
                    return c;
            }).ToArray());
Console.WriteLine(n);  // adc
w.b
  • 11,026
  • 5
  • 30
  • 49