473,396 Members | 1,838 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,396 software developers and data experts.

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.ToBase64String(full_enc);

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 5173
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********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.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.ToBase64String(full_enc);

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********@gmail.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********@gmail.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.ToBase64String(full_enc);

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(Convert.ToBase64String(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.ToBase64String(full_enc).Replace('+','*');

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

<ri********@gmail.comha scritto nel messaggio
news:11**********************@h48g2000cwc.googlegr oups.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.ToBase64String(full_enc);

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(Convert.ToBase64String(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.ToBase64String(full_enc).Replace('+','*');

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

<ri********@gmail.comha scritto nel messaggio
news:11**********************@h48g2000cwc.googlegr oups.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.ToBase64String(full_enc);

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********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.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.ToBase64String(full_enc);

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
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...
6
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...
8
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...
6
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...
22
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; ...
7
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...
9
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...
8
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...
13
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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,...

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.