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

Adding characters to a string : Error 'string.this[int]' cannot be assigned to -- it is read only

I am attempting to copy a portion of a string into another string. Say
I have "abcdef" in string 'a' and want to copy only "bcd" into a new
string 'b'. The following function came to mind:

public string GetText(string a, int start, int end)

{
int i;
string b;

for (i=0;i<(end-start);i++)
{
b[i]=(a[start+i]);
}

return b;
}
At compile time, I am getting the error: "Property or indexer
'string.this[int]' cannot be assigned to -- it is read only"

Which makes sense, since I am attempting to increase the sisce of a
fixed string. Is there an easier way to do this?

- j. oliver
Nov 15 '05 #1
4 11855
Hi

Use StringBuilder instead of String.
Then you can use StringBuilder.Append();

HTH
Ravikanth[MVP]

-----Original Message-----
I am attempting to copy a portion of a string into another string. SayI have "abcdef" in string 'a' and want to copy only "bcd" into a newstring 'b'. The following function came to mind:

public string GetText(string a, int start, int end)

{
int i;
string b;

for (i=0;i<(end-start);i++)
{
b[i]=(a[start+i]);
}

return b;
}
At compile time, I am getting the error: "Property or indexer'string.this[int]' cannot be assigned to -- it is read only"
Which makes sense, since I am attempting to increase the sisce of afixed string. Is there an easier way to do this?

- j. oliver
.

Nov 15 '05 #2
Is there an easier way to do this?


String.Substring()

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #3
I used a PERL primer for my knowledge of RegExp.
Many to be found as ebooks online, for free. Maybe useful ?

Best
Stu

"J. Oliver" <jo*********@spamex.com> wrote in message
news:9d**************************@posting.google.c om...
I am attempting to copy a portion of a string into another string. Say
I have "abcdef" in string 'a' and want to copy only "bcd" into a new
string 'b'. The following function came to mind:

public string GetText(string a, int start, int end)

{
int i;
string b;

for (i=0;i<(end-start);i++)
{
b[i]=(a[start+i]);
}

return b;
}
At compile time, I am getting the error: "Property or indexer
'string.this[int]' cannot be assigned to -- it is read only"

Which makes sense, since I am attempting to increase the sisce of a
fixed string. Is there an easier way to do this?

- j. oliver

Nov 15 '05 #4
Hi!

Well, in your case you could simply use the string.Substring(int index, int
length) to get the string you need.

string b = a.Substring(start, end-start);

Fairly simple.

If you on the other hand want to add characters and so on you could alway
use the StringBuilder (System.Text)

System.Text.StringBuilder sb = new StringBuilder(string.Empty); //A new
totally empty stringbuilder

for (int i = start; i < end; i++)
{
sb.Append(str[i])
}

return sb.ToString(); //returns the string you wanted

I hope this helps!

//Mikael

"Stu Banter" <x2**************@westerterp.com> wrote in message
news:3f***********************@dreader9.news.xs4al l.nl...
I used a PERL primer for my knowledge of RegExp.
Many to be found as ebooks online, for free. Maybe useful ?

Best
Stu

"J. Oliver" <jo*********@spamex.com> wrote in message
news:9d**************************@posting.google.c om...
I am attempting to copy a portion of a string into another string. Say
I have "abcdef" in string 'a' and want to copy only "bcd" into a new
string 'b'. The following function came to mind:

public string GetText(string a, int start, int end)

{
int i;
string b;

for (i=0;i<(end-start);i++)
{
b[i]=(a[start+i]);
}

return b;
}
At compile time, I am getting the error: "Property or indexer
'string.this[int]' cannot be assigned to -- it is read only"

Which makes sense, since I am attempting to increase the sisce of a
fixed string. Is there an easier way to do this?

- j. oliver


Nov 15 '05 #5

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

Similar topics

2
by: Liber Almeida | last post by:
I have a problem when install a Visual C#. Net application, using Crytal Reports, under Windows98: error: "cannot find KeycodeV2.dll or invalid Keycode" Help me please, Thank you, Liber...
6
by: juli | last post by:
I declared: public delegate void PaintEventHandler(object objSender,PaintEventArgs pea); and this.Paint+=new PaintEventHandler(MyPaintHandler); and the: static void MyPaintHandler(object...
3
by: Dmitry Jouravlev | last post by:
Hi, I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be...
0
by: weixian_shen | last post by:
I'm trying to call my DLL written in C, and got the error: Cannot marshal field 'b' of type 'mystruct': There is no marshaling support for this type. The 2 functions in the DLL are: void...
1
by: Vycka | last post by:
Hello, There is a enterprise web application that is based on asp.net technologies and works on Microsoft IIS. The total number of users is 850. When the load of system gets very high, the...
6
by: baret bonden | last post by:
I get :Value of type 'String' cannot be converted to '1-dimensional array of String' refering to curitem Dim curItem As String curItem = ListBox1.SelectedItem TextBox1.Text = curItem ...
1
by: spud379 | last post by:
i am doing an assignment in vb.net. i have to select at random from and array what i have is dim country(29) as string dim random as new random txtcountry.text = random.next(country) I...
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
5
by: Just Another Victim of the Ambient Morality | last post by:
I have a peculiar bug in my PHP code. It looks something like this: Fatal error: Cannot redeclare func_name() (previously declared in script.php:57) in script.php on line 57 I've done a...
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: 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:
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.