469,315 Members | 1,503 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

String Manipulation

How can I change a particular char at a given index....
I want to do something like:

strText[2] = 'b';

But apparently the "strText[2]" is readonly?
Is there another way to get access to a particular character?

Jan 4 '06 #1
4 2948
You cannot. Strings are immutable, even for replacing a character that
already exists.

"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
How can I change a particular char at a given index....
I want to do something like:

strText[2] = 'b';

But apparently the "strText[2]" is readonly?
Is there another way to get access to a particular character?

Jan 4 '06 #2
INeedADip <in*******@gmail.com> wrote:
How can I change a particular char at a given index....
I want to do something like:

strText[2] = 'b';

But apparently the "strText[2]" is readonly?
Is there another way to get access to a particular character?


Strings are immutable in .NET. If you need a different sequence of
characters, you need a different string object.

The best way of doing this depends on exactly what you need to do - do
you genuinely only need to change a single character, or is that just
an example?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 4 '06 #3
"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
How can I change a particular char at a given index....
I want to do something like:

strText[2] = 'b';

But apparently the "strText[2]" is readonly?
Is there another way to get access to a particular character?


Use a StringBuilder...

StringBuilder textBuilder = new StringBuilder(256);

textBuilder.Append(strText);
textBuilder.Remove(2, 1);
textBuilder.Insert('b', 2);
strText = textBuilder.ToString();

I hope this helps.

carl
Jan 4 '06 #4
Hi
You can use StringBuilder instead of string. And Change Or access
purticular character.

e.g
StringBuilder sb = new StringBuilder("My Test String");
sb[5] = 'x';
String str = sb.ToString();

Jan 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by tshad | last post: by
29 posts views Thread by zoro | last post: by
4 posts views Thread by WaterWalk | last post: by
10 posts views Thread by micklee74 | last post: by
3 posts views Thread by crprajan | last post: by
3 posts views Thread by frankeljw | last post: by
22 posts views Thread by mann_mathann | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.