modify a char in string

  • Thread starter Thread starter Zeng
  • Start date Start date
Z

Zeng

Hello,

Is it true that in C# we can't modify directly a character in a string? I
can't find the method anywhere.

I would expect something like this should work,but it doesn't

string myStr = "blah';
myStr[ 3 ] = 'e';

Thanks in advance for your comment.
 
Cudnt find a direct method either but here is one of the indirect way

// Not Compiled-might have syntax errors
string str = "blah";
char[] arr = str.ToCharArray();
arr[3]='e';
str = new string( arr );

HTH
rawCoder
 
Hi Zeng,

It is correct, you cannot modify a string directly, but will need to
reassemble it, using SubString/Insert/Remove etc.

string myStr = "blah";
int pos = 3;
myStr = myStr.Substring(0, pos) + 'c' + myStr.Substring(++pos,
myStr.Length - pos);

Or you can do as rawCoder said and use ToArray and change the char[]
 
Hello Zeng,

you can't use the indexer on the string object to set chars, only read them.

Hope this helps,
RBischoff
-----------------------------------------
http://msdn.microsoft.com/visualc/ (VC++ HOME)
http://www.mvps.org/vcfaq/ (C++ FAQ)
http://www.winterdom.com/mcppfaq/ (MC++ FAQ)
http://msdn.microsoft.com/visualc/whidbey/ (CLI)

Z> Hello,
Z>
Z> Is it true that in C# we can't modify directly a character in a
Z> string? I can't find the method anywhere.
Z>
Z> I would expect something like this should work,but it doesn't
Z>
Z> string myStr = "blah';
Z> myStr[ 3 ] = 'e';
Z> Thanks in advance for your comment.
Z>
 
Thanks, now that makes me really curious, does anyone know why it was
designed that way? There must be a reason for not allowing it.
 
Hi Zeng,

Strings are special because they are stored in an intern pool. All
strings in the pool are unique and each time you create a new string
object containing a string value that is already inside the pool you will
get a reference to the old string instead of creating a new string object.

Microsoft calls it an intern pool of unique strings. The documentation of
String.Intern explains it much better.

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemstringclassinterntopic.asp
 
Zeng said:
Thanks, now that makes me really curious, does anyone know why it was
designed that way? There must be a reason for not allowing it.

A string, once created, is immutable. That means that a string cannot be
changed, at least not easily or using the normal access methods. Any method
that looks like it is modifying a string is actually building a new string.
Among other things, this allows for memory effeciencies that couldn't be
achieved otherwise.

More info on strings in .NET can be found at
http://www.yoda.arachsys.com/csharp/strings.html.
 
I think the following is another solution, but I have not tested it !

StringBuilder sb = new StringBuilder("blah");
sb[3] = 'e';
String myStr = sb.ToString();

Hope that helps.
jmd
 
Morten Wennevik said:
Strings are special because they are stored in an intern pool. All
strings in the pool are unique and each time you create a new string
object containing a string value that is already inside the pool you will
get a reference to the old string instead of creating a new string object.

No you won't. Interning only happens when you tell it to, or when
you're using string literals. Here's proof of that:

using System;

class Test
{
static void Main()
{
string x = "1";
string y = new string(new char[]{'1'});
string z = string.Intern(y);

Console.WriteLine (object.ReferenceEquals(x,y));
Console.WriteLine (object.ReferenceEquals(y,z));
Console.WriteLine (object.ReferenceEquals(x,z));
}
}

x is guaranteed to be interned because it's a literal.
z is guaranteed to be the same as x because it's the interned value of
an equivalent string.
y is guaranteed to be different to both of them because it's a
reference to a *new* string, not an interned one.
 
Ah, I didn't know that.


Would they be referencing the same internal string, since *new* isn't
specified?

Morten Wennevik said:
Strings are special because they are stored in an intern pool. All
strings in the pool are unique and each time you create a new string
object containing a string value that is already inside the pool you
will
get a reference to the old string instead of creating a new string
object.

No you won't. Interning only happens when you tell it to, or when
you're using string literals. Here's proof of that:

using System;

class Test
{
static void Main()
{
string x = "1";
string y = new string(new char[]{'1'});
string z = string.Intern(y);
Console.WriteLine (object.ReferenceEquals(x,y));
Console.WriteLine (object.ReferenceEquals(y,z));
Console.WriteLine (object.ReferenceEquals(x,z));
}
}

x is guaranteed to be interned because it's a literal.
z is guaranteed to be the same as x because it's the interned value of
an equivalent string.
y is guaranteed to be different to both of them because it's a
reference to a *new* string, not an interned one.
 
Morten Wennevik said:
Ah, I didn't know that.

Would they be referencing the same internal string, since *new* isn't
specified?

Would what be referencing the same internal string?

(Note that when constructing y, I'm using the character literal '1' -
there isn't a string constructor which just takes another string.)
 
Oh, hehe, I was writing a question regarding the two strings

string a = "Hello World";
string b = "Hello World";

.... but then I noticed you already mentioned this case.
I must have missed a line when deleting it :P
 
Back
Top