A simple ForEach Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    #1

    A simple ForEach Question

    Hi

    I have split text into an array using char[] and ToCharArray()

    I want to do the below code using a loop instead of writing out each condition as an if statement. Would this be best as a loop or is it best to do each one individually? Also, if it is best to do as a loop, how??

    if array[0] = "x" then do this.
    if array[1] = "x" then do this
    if array[2] = "x" then do this
    if array[3] = "x" then do this
    if array[4] = "x" then do this

    My second question is how to join 3 arrays into a string. EG, if array 0,1,2 each represenst one letter it could spell the word egg (where 0=e 1 = e 2 = g). I would then need to say

    if (group of arrays 0,1,2 = egg)
    {
    then do this
    }

    If any one can help, thank you

    Dave
  • kunal pawar
    Contributor
    • Oct 2007
    • 297

    #2
    tried out
    for(i=0;i<array .length;i++)
    {
    if(array(i)='x' )
    then do this
    }

    Comment

    • DaveRook
      New Member
      • Jul 2007
      • 147

      #3
      Thank you Kunal

      It is really kind of you to help me!

      Thank you again

      Dave

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You're not in VB, arrays are denoted with the correct [ and ] and not ( and )
        Switch to square brackets when referencing the index of an array.


        Also, since you're working with characters and strings you could do this:
        Code:
        string puttogether="";
        puttogether+=mychararray[0];
        puttogether+=mychararray[2];
        puttogether+=mychararray[4];
        And the string would have your characters

        Comment

        Working...