473,385 Members | 2,029 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,385 software developers and data experts.

string problem

string s("0101");

I can't change it into s=1101
by

s[0]='1';

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

thank you for your help

Nov 18 '06 #1
9 1694
"Mosquito Man" <mo**********@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
string s("0101");

I can't change it into s=1101
by

s[0]='1';

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

Perhaps the StringBuilder class is more suitable for your needs.
Nov 18 '06 #2
Hi,

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

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

--
Dave Sexton

"Mosquito Man" <mo**********@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
string s("0101");

I can't change it into s=1101
by

s[0]='1';

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

thank you for your help

Nov 18 '06 #3
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

Mosquito Man wrote:
string s("0101");

I can't change it into s=1101
by

s[0]='1';

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

thank you for your help
Nov 18 '06 #4
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.
--
http://www.markdawson.org
"Mosquito Man" wrote:
string s("0101");

I can't change it into s=1101
by

s[0]='1';

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

thank you for your help

Nov 18 '06 #5
Truong Hong Thi wrote:
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.

--

www.midnightbeach.com/.net
What you need to know.
Nov 18 '06 #6
On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <mo**********@yahoo.com>
wrote:
>string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this[i] 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

Nov 18 '06 #7
"rossum" <ro******@coldmail.comwrote in message
news:8b********************************@4ax.com...
On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <mo**********@yahoo.com>
wrote:
>>string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this[i] 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.

Nov 18 '06 #8
On Sat, 18 Nov 2006 22:11:43 +0100, "Willy Denoyette [MVP]"
<wi*************@telenet.bewrote:
>"rossum" <ro******@coldmail.comwrote in message
news:8b********************************@4ax.com.. .
>On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <mo**********@yahoo.com>
wrote:
>>>string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this[i] 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

Nov 19 '06 #9
rossum <ro******@coldmail.comwrote:
On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <mo**********@yahoo.com>
wrote:
string s("0101");

I can't change it into s=1101
by

s[0]='1';

because string.this[i] 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".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 19 '06 #10

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

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
4
by: MooMaster | last post by:
After some google searching on the forum I couldn't find any topics that seemed to relate exactly to my problem, so hopefully someone can help me out... I'm running python 2.4.1 on a local Win2K...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
5
by: ThatVBGuy | last post by:
Hello All, I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.