string problem

  • Thread starter Thread starter Mosquito Man
  • Start date Start date
M

Mosquito Man

string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this is read-only, no assignment is allowed :-(

thank you for your help
 
Mosquito Man said:
string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this is read-only, no assignment is allowed :-(



Perhaps the StringBuilder class is more suitable for your needs.
 
Hi,

System.String is immutable. A better choice would be to use a StringBuilder:

StringBuilder s = new StringBuilder("0101");
s[0] = '1';
 
Hi,

If you want to change the string that way, you'd better use
StringBuilder class.

string s = "0101";
StringBuilder builder = new StringBuilder(s);
builder [0] = '1';
builder [3] = '0';
s = builder.ToString();

An alternative is use String.ToCharArray. You than can set each char to
whatever you want, then create a new string, past the array to string
constructor.

string s = "0101";
char[] chars = s.ToCharArray();
chars[0] = '1';
chars[3] = '0';
s = new String(chars);

Thi
http://thith.blogspot.com
 
Hi,
strings are immutable, so you cannot change the contents of a string once
it has been defined. If you want to change values you are going to have to
use some combination of .SubString .Replace etc to replace individual values.

HTH
Mark.
 
Truong said:
If you want to change the string that way, you'd better use
StringBuilder class.
An alternative is use String.ToCharArray. You than can set each char to
whatever you want, then create a new string, past the array to string
constructor.

string s = "0101";
char[] chars = s.ToCharArray();
chars[0] = '1';
chars[3] = '0';
s = new String(chars);

Of course, this latter is exactly what the StringBuilder class is
doing. All you gain by taking the manual approach is saving a very few
cycles by not creating a StringBuilder instance ... at the cost of
bigger, more complex source code that is harder to maintain.
 
string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this is read-only, no assignment is allowed :-(

thank you for your help

Others have pointed you to the StringBuilder class, which is probably
the solution you are looking for.

An alternative is to drop into unsafe code and use pointers which do
allow you to change otherwise "immutable" strings:

unsafe void OverwriteChar(string text, int pos, char newChar) {
fixed (char* cPointer = text) {
cPointer[pos] = newChar;
}
}

I use this technique to overwrite security sensitive strings before
releasing them for garbage collection.

rossum
 
rossum said:
string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this is read-only, no assignment is allowed :-(

thank you for your help

Others have pointed you to the StringBuilder class, which is probably
the solution you are looking for.

An alternative is to drop into unsafe code and use pointers which do
allow you to change otherwise "immutable" strings:

unsafe void OverwriteChar(string text, int pos, char newChar) {
fixed (char* cPointer = text) {
cPointer[pos] = newChar;
}
}

I use this technique to overwrite security sensitive strings before
releasing them for garbage collection.

rossum



V2 of the framework provides a SecureString (System.Security) which has the advantage to be
encrypted when live and deleted from memory when detached, no need for unsafe code.
Willy.
 
rossum said:
string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this is read-only, no assignment is allowed :-(

thank you for your help

Others have pointed you to the StringBuilder class, which is probably
the solution you are looking for.

An alternative is to drop into unsafe code and use pointers which do
allow you to change otherwise "immutable" strings:

unsafe void OverwriteChar(string text, int pos, char newChar) {
fixed (char* cPointer = text) {
cPointer[pos] = newChar;
}
}

I use this technique to overwrite security sensitive strings before
releasing them for garbage collection.

rossum



V2 of the framework provides a SecureString (System.Security) which has the advantage to be
encrypted when live and deleted from memory when detached, no need for unsafe code.
Willy.

Thanks for that, I used the technique in V1 and haven't needed it
since the move to V2.

rossum
 
rossum said:
string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this is read-only, no assignment is allowed :-(

thank you for your help

Others have pointed you to the StringBuilder class, which is probably
the solution you are looking for.

An alternative is to drop into unsafe code and use pointers which do
allow you to change otherwise "immutable" strings:

unsafe void OverwriteChar(string text, int pos, char newChar) {
fixed (char* cPointer = text) {
cPointer[pos] = newChar;
}
}

I use this technique to overwrite security sensitive strings before
releasing them for garbage collection.


Note that that can produce *very* disturbing results - in this case, if
the literal "0101" were changed, *all* string literals in the code
which should be "0101" would then be "1101".
 

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

Back
Top