char replace in string

B

Bob

Is there a way to replace a character in a string without having the
framework allocate a new string the way string.Replace does?

I need the below code to work, alothough it says the indexer is read only.

string B = "412345";
B[0]='0';

The reason is for pure performance. I am doing a lot of changes and can't
have it allocate a new string every time its modified.

Bob
 
J

Jon Skeet [C# MVP]

Bob said:
Is there a way to replace a character in a string without having the
framework allocate a new string the way string.Replace does?

No. Strings are immutable.
I need the below code to work, alothough it says the indexer is read only.

string B = "412345";
B[0]='0';

The reason is for pure performance. I am doing a lot of changes and can't
have it allocate a new string every time its modified.

You may wish to consider using a StringBuilder instead, or a char array
if that can perform all the changes you require.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top