char replace in string

  • Thread starter Thread starter Bob
  • Start date Start date
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
 
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
 
Back
Top