tags:

views:

165

answers:

8

Hi,

I want to do this:

string s = "abc";
s[1] = 'x';

and s will become "axc". However, it seems that string[i] only has a getter and has no setter. The compiler gives me the following error:

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

I guess I could make a loop and change the char i want. but i was just wondering if there is an easy way to do it? And why there isn't a setter for string[i]?

Thanks in advance.

+14  A: 

Strings are immutable, so you have to make a char[] array, change it, then make it back into a string:

string s = "foo";
char[] arr = s.ToCharArray();
arr[1] = 'x';
s = new string(arr);
Callum Rogers
thanks. second line should be char[] arr .... tho :)
StarCub
I saw it and corrected it. Thanks anyway :)
Callum Rogers
+1  A: 

I don't think you can do this in C#, as the string cannot be altered (just destroyed and recreated). Have a look at the StringBuilder class.

pm_2
+2  A: 

(Your example is slightly wrong: s[2] = 'x' should change it to "abx".)

No you can't, since strings are immutable, you have to create a new string:

http://en.wikipedia.org/wiki/Immutable_object

You should use a method that returns a new string with the desired modification.

Hope that helps!

Kieren Johnstone
of course! thanks for pointing that out! lol
StarCub
+12  A: 

Strings are immutable which is why there's no setter, you can however use a string builder:

StringBuilder s = new StringBuilder("abc");

s[1] = 'x';
JLWarlow
+2  A: 

Remember, in managed and safe .Net, strings are immutable, so even if you could do the above, you'd really be creating a new copy of the string with the replacement.

If you are only replacing one character, a simple loop is probably your best bet.

However, if you are going to make multiple replacements, consider using a StringBuilder:

  string s = "abc";
  var stringBuilder = new StringBuilder(s);
  stringBuilder[1] = 'x';
  s = stringBuilder.ToString();
Rob Levine
should be stringBuilder[1] = 'x' not stringBuilder[2]. This returns abx.
btlog
@btlog - Corrected. Ta.
Rob Levine
A: 

What's about this?

string originalString = "abc";

        var index = 1;
        char charToReplace = 'x';

        var newString = string.Format("{0}{1}{2}", originalString.Substring(0, index), charToReplace, originalString.Substring(index + 1));
DrakeVN
How about string newString = originalString.Remove(index, 1).Insert(index, charToReplace)
btlog
A: 

yes in c# string can not be altered.

but we can try this

string s = "abc";
s = s.Replace('b', 'x');
Console.WriteLine(s);

answer will be "axc". as this will replace the old string with new string.

Kinjal
But if the string contains several "b"s and you only want to replace the first, this won't work.
ChrisF
+1  A: 

Why not do this if you're using some Linq

private string ConvertStr(string inStr , int inIndex , char inChar)
{
char[] tmp = inStr.ToCharArray();
tmp.SetValue(inChar , inIndex);
return new string(tmp);
}

That should let you replace whatever char you want with whatever char you want.

PSU_Kardi