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

Updating an element of a string

Here is another simple question (I'm asking them because I couldn't find a way by myself):

Suppose I have a string

x = "Daniweb"

When I try to update an element like :

x[3] = k


The Visual Studio says it is read only and gives error.

How can I solve this problem?
Thanks..

newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Strings are immutable in C#. If you want something more like an array, use a StringBuilder object:

var x = new StringBuilder("Daniweb");

x[3] = 'k';

Console.WriteLine(x.ToString());
deceptikon
Indubitably
Administrator
633 posts since Jan 2012
Reputation Points: 119
Solved Threads: 106
 

Thank you! That really helped!

newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You