CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2008
    Posts
    59

    Question 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

  2. #2
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: String Manipulation query

    Quote Originally Posted by fsdama View Post
    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...

  3. #3
    Join Date
    Oct 2008
    Posts
    59

    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?

  4. #4
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: String Manipulation query

    Quote Originally Posted by fsdama View Post
    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.

  5. #5
    Join Date
    Oct 2008
    Posts
    59

    Question 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.

  6. #6
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    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
  •  





Click Here to Expand Forum to Full Width

Featured