|
-
March 24th, 2009, 09:47 AM
#1
String Manipulation query
Hi,
I am having difficulty understand the second line of the code below:
Code:
string myString = "A string";
char myChar = myString[1];
The first line is creating and declaring a string, but I am not sure about the second line. The book I am following is using this code as an example. The book says "Its worth noting that a string type variable can be treated as a read only array of char variables. This means that you can access individual characters using syntax like in the code above.
Although its a simple explanation I still dont understand it. What is the code trying to do and what is the ' [1]' doing at the end of myString?
Thanks in advance
-
March 24th, 2009, 09:51 AM
#2
Re: String Manipulation query
 Originally Posted by fsdama
Hi,
Code:
string myString = "A string";
char myChar = myString[1];
What is the code trying to do and what is the ' [1]' doing at the end of myString?
This is just returning second character in myString variable (space in this case).
[1] is index of character array of this text (actually default property, but never mind). If you put [2], you would get 's', if you put [3], you would get 't', and so on...
-
March 24th, 2009, 09:58 AM
#3
Re: String Manipulation query
Thanks for the quick answer. I now understand it. But how come the array was never declared? Is this because by default, the string type variable can be treated as a read only array of char variables? or am I missing something else?
-
March 24th, 2009, 10:18 AM
#4
Re: String Manipulation query
 Originally Posted by fsdama
Thanks for the quick answer. I now understand it. But how come the array was never declared? Is this because by default, the string type variable can be treated as a read only array of char variables? or am I missing something else?
It is not array as such. It is implemented as C# indexer. This property looks exactly like array (in this case) and it is done for convenience. You can view it as syntactic sugar.
-
March 26th, 2009, 12:05 PM
#5
Re: String Manipulation query
Thanks for your reply.
I have another similar scenario I am having trouble understanding.
Code:
string myString = "A string";
char[] myChars = myString.ToCharArray();
The book I am following doesnt explain this very well. It says that the difference between this code above and the code in my first post on this thread is that ToCharArray() allows you to 'get a char array that you can write to'. I've looked up the TocCharArray() command on the web but still dont understand it. Can someone help?
Thanks.
-
March 26th, 2009, 02:12 PM
#6
Re: String Manipulation query
This will make array of characters. As a help to you, this array will be prefilled with data from string. You can change it as you did in first example (both indexers and arrays share same syntax) but at cost of doubling amount of memory needed. I do not find need for this very often in programs since any modification (except for in-place changes) will require new memory allocations.
To sum it all. Just stick with string and you'll be find. Once you reach loops, take a look at StringBuilder and you have covered enough ground to make something useful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|