473,412 Members | 4,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

modify a char in string

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.
Nov 17 '05 #1
11 1798
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

"Zeng" <Ze******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
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.

Nov 17 '05 #2
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[]

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3
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>

Nov 17 '05 #4
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.
"RBischoff" <ry****@NogmailSpAmPl.EaSe.com> wrote in message
news:28*********************@msnews.microsoft.com. ..
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>

Nov 17 '05 #5
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...nterntopic.asp
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #6
Zeng wrote:
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.
--
Tom Porterfield
Nov 17 '05 #7
jmd
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

"Zeng" <Ze******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
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.

Nov 17 '05 #8
Morten Wennevik <Mo************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Ah, I didn't know that.
Would they be referencing the same internal string, since *new* isn't
specified?

On Fri, 8 Apr 2005 19:48:50 +0100, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
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.


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #10
Morten Wennevik <Mo************@hotmail.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
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
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Venkat | last post by:
Greetings to All, I have the following file and i need to modify it with the contents of an array File1.txt Name Location Points Grade Venkat,Newyork,100,A Jack,LA,12,C
28
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one...
13
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
12
by: Michael B Allen | last post by:
Is it legit to modify static data like the following code? #include <stdlib.h> #include <stdio.h> struct tbl { int i; char *s; };
7
by: Jchick | last post by:
Firstly, I am a noobie with some exposure to VB.net Secondly, I am tasked with trying to do the following: I have a CSV file that I'd like to have a vb.net program make a change to: Here is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.