I am coming from c++ and its nice STL. In C++ I could do this

<include> string

...
...
int nMyInt = 5;
string sMyString;
sMyString[5] = "b";

Ive tried this approach in c# but doing this way gives me this error

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

And I understand the error... is there anyway to do this in C#??

Recommended Answers

All 2 Replies

String is immutable (constant/readonly)object. Its value cannot be modified once it has been created.

Use StringBuilder class (It's mutable object).

StringBuilder sb = new StringBuilder();
sb.Append("HeleoWorld");
sb[3] = 'l';
Console.WriteLine(sb);
commented: Thanks So Much +1

Thanks so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.