472,119 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

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 3067
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.