473,805 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11899
Hi

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

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.Substrin g()

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*********@sp amex.com> wrote in message
news:9d******** *************** ***@posting.goo gle.com...
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.Substrin g(int index, int
length) to get the string you need.

string b = a.Substring(sta rt, 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.Str ingBuilder sb = new StringBuilder(s tring.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.c om> wrote in message
news:3f******** *************** @dreader9.news. xs4all.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*********@sp amex.com> wrote in message
news:9d******** *************** ***@posting.goo gle.com...
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
7664
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 Almeida.
6
14379
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 objSender,PaintEventArgs pea) { }
3
4144
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 loaded. The error contains the correct path to c2.dll (and it is definately there). This only happens on some projects and only when "whole program optimization" option is turned on. If i turn off this option, the problem goes away. I have other...
0
2104
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 unpack(mystruct* str, void* in_buf); void pack(mystruct* str, void* out_buf);
1
5852
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 system stops working and all users are receiving an error (see at the bottom). Doing IISRESET or restarting server helps (but just until the next big load). Additional information: - Server OS MS Windows Server 2003 Standard edition SP1
6
38076
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 vcar = curItem
1
1439
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 keep getting that this error 1-demensional array string cannot be converted to integer
13
3421
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. The first column is always an English
5
3677
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 search and determined that "func_name" really is unique. Besides, how is this even possible? It was previously declared on the same line it's declared? Any help is appreciated. Thank you!
0
9716
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
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
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
10360
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...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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();...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.