Results 1 to 2 of 2

Thread: ToCharArray vs. String[i]

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    ToCharArray vs. String[i]

    If I want to loop through the characters in a string, would it matter at all (as far as efficiency is concerned) whether I access the characters with brackets or if I convert the whole thing to a character array first and then access the characters?
    ie:

    string myStr = "MrPolite rules the world";
    for (int i=0; i<myStr.Length; i++)
    Console.WriteLine (myStr[i]);

    vs.

    string myStr = "MrPolite rules the world";
    char[] chrs = myStr.ToCharArray();
    for (int i=0; i<chrs.Length; i++)
    Console.WriteLine (chrs[i]);

    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I THINK , the first way is more efficient since string is actually char array .So , there's no need to convert it to char array . You just access it by index. At least , this would make sense in C/C++ .

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