473,732 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String read only error

Hello,

Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".
string enc_msg = Convert.ToBase6 4String(full_en c);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg[i] == '+' )
{
enc_msg[i] = '*';
}

}
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Regards,
Rithesh Swamy

Jul 27 '06 #1
6 5195
hi,
the string object is immutable. You cannot call it as an array and
change it. Try using the .NET 'String.Join' object instead ( note the
capital S) . Or simply create a new string object.

James Jenkins
http://www.tamarsolutions.co.uk

<ri********@gma il.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hello,

Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".
string enc_msg = Convert.ToBase6 4String(full_en c);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg[i] == '+' )
{
enc_msg[i] = '*';
}

}
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Regards,
Rithesh Swamy

Jul 27 '06 #2
ri********@gmai l.com wrote:

Hi,
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes
Strings cannot be changed. You'll have to create a new one every time
you want to make changes. Also there already is a String.Replace
function, you shouldn't implement your own like you did with the for loop:

enc_msg = enc_msg.Replace ('+', '*');

hth,
Max
Jul 27 '06 #3
ri********@gmai l.com wrote:
Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".

string enc_msg = Convert.ToBase6 4String(full_en c);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg[i] == '+' )
{
enc_msg[i] = '*';
}

}
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes
Strings in .NET are an immutable type - once you've constructed an
instance, you can't ever change it. Use a StringBuilder instead, if you
want to index the string and assign directly - but you'll still end up
with a copy.

By the way, one way to do what you're trying to do is this:

enc_msg = enc_msg.Replace ('+', '*');

-- Barry

--
http://barrkel.blogspot.com/
Jul 27 '06 #4
strings are immutable, thus the indexer of the string object is defined as
public char this[int index] { get; }

Change the enc_msg from string to StringBuilder and you are ok.

StringBuilder enc_msg = new StringBuilder(C onvert.ToBase64 String(full_enc ));

OR

You could use string.Replace to change all '+' to '*'. In this way you do
not need the loop.

string enc_msg = Convert.ToBase6 4String(full_en c).Replace('+', '*');

Of course this creates temporary objects so it increases memory usage a
bit..

<ri********@gma il.comha scritto nel messaggio
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hello,

Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".
string enc_msg = Convert.ToBase6 4String(full_en c);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg[i] == '+' )
{
enc_msg[i] = '*';
}

}
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Regards,
Rithesh Swamy

Jul 27 '06 #5
Thanks a lot guys, both the Replace and StringBuilder solved my
problem.

Regrads,
Rithesh Swamy

Laura T wrote:
strings are immutable, thus the indexer of the string object is defined as
public char this[int index] { get; }

Change the enc_msg from string to StringBuilder and you are ok.

StringBuilder enc_msg = new StringBuilder(C onvert.ToBase64 String(full_enc ));

OR

You could use string.Replace to change all '+' to '*'. In this way you do
not need the loop.

string enc_msg = Convert.ToBase6 4String(full_en c).Replace('+', '*');

Of course this creates temporary objects so it increases memory usage a
bit..

<ri********@gma il.comha scritto nel messaggio
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hello,

Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".
string enc_msg = Convert.ToBase6 4String(full_en c);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg[i] == '+' )
{
enc_msg[i] = '*';
}

}
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Regards,
Rithesh Swamy
Jul 28 '06 #6
Like Java, string type is Immutable in .NET. So you cannot change it. try
stringbuilder.

<ri********@gma il.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hello,

Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".
string enc_msg = Convert.ToBase6 4String(full_en c);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg[i] == '+' )
{
enc_msg[i] = '*';
}

}
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Regards,
Rithesh Swamy

Jul 28 '06 #7

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

Similar topics

2
2564
by: Senthoorkumaran Punniamoorthy | last post by:
I am printing these information. print string.lower(info_res) print string.lower(md5sum(f_md5)) print len(string.lower(info_res)) print len(string.lower(md5sum(f_md5))) print str(string.lower(md5sum(f_md5)) == string.lower(info_res)) and the output are
6
3507
by: Chris Connett | last post by:
I have an interesting problem with eval(). ---Background Info--- The program I'm working on launches a separate process with a popen to do some highly specialized processing of input, then this process leaves resultant data structured in files, in python syntax, at a known absolute path, which is then read in, eval()'ed, and processed further. It works fine when there are no errors in processing by this external program, but when there...
8
4062
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
6
8143
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional character array, but I'd really like to read it into a vector of strings. Here's how I'm doing the read into the char array: int main() { string str1("booger"); string str2("test");
22
13327
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
7
4360
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
9
7134
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
8
2684
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when returning null pointers.) It looks pretty watertight to me, but my version of lint complains about use of deallocated pointers, etc. Is this code completely safe on all input, or have I missed something? /* Header files included in the program...
13
3217
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
2
2178
by: wingleader | last post by:
Hi. Got such a problem. I wrote a url decoding programm. The main idea is, that programm take char by char from incoming string, decides what changes must be made, and then inserts them to enother. But, seems, like there is a logical error, because it runs endless circles and never comes to an end. Could someone fix an error?string Utils::urldecode(string incoming) { string outgoing;//RESULT STRING char current;//CURRENT CHAR char...
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.