954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# equivalent of C++ SomeString[3] = "a"

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 errorProperty 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#??

wade2462
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

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);
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Thanks so much.

wade2462
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: