I get an error when using this code:
it says the indexer is read only. How can I accomplish the same thing?Code:string str = "ahe string"; str[0]='T';
Hi >> Saint
Shouldnt you declare the array like pascal or so? I dont know coz i dont C# but just a curiosity!
Yeah, Strings in .NET are immutable - you can't change them without creating a new String. So, if you attempted to change one of the characters, you'll have a new string - but no reference to it.
What you want to do is declare a char array, and convert the string to a char array. Then you can change the individual characters:
Code:string str = "ahe string"; char[] c = str.ToCharArray(); chr[0] = 't'; string newstr = new String(chr); // Convert back to string
Thanks brackett, that worked fine.
Hi >> Saint
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks