Click to See Complete Forum and Search --> : String Manipulation query
fsdama
March 24th, 2009, 09:47 AM
Hi,
I am having difficulty understand the second line of the code below:
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
jmedved
March 24th, 2009, 09:51 AM
Hi,
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...
fsdama
March 24th, 2009, 09:58 AM
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?
jmedved
March 24th, 2009, 10:18 AM
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 (http://msdn.microsoft.com/en-us/library/2549tw02(VS.71).aspx). This property looks exactly like array (in this case) and it is done for convenience. You can view it as syntactic sugar.
fsdama
March 26th, 2009, 12:05 PM
Thanks for your reply.
I have another similar scenario I am having trouble understanding.
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.
jmedved
March 26th, 2009, 02:12 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.