Results 1 to 3 of 3

Thread: How to split the string in simple way

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    india
    Posts
    273

    How to split the string in simple way

    I have a string with delimiters.The folowing code runs fine & I achived the target i want but can i get a 0th and 1st array directly when i split the string .how do i modify the innermost foreach loop.

    see he code below:



    columnFields="dww,243\n\r32,sedwe\n\r";

    foreach (string strField in columnFields.Split("\n"))
    {
    int iFld=0;
    foreach (string strFld in strField.Split(","))
    {
    if(iFld==0)
    {
    sVal1=strFld; //First value ie "dww"
    iFld+=1;
    }
    else
    {
    sVal2 =strFld.Replace("\r","");//second value ie."243"
    }
    }
    }


    Help expected..
    regards,



    PPCC

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Why don't you just do this:
    Code:
    columnFields="dww,243\n\r32,sedwe\n\r";
    
    string[] myFirstSplit = columnFields.Split("\n");
    string[] myFirstSetSplit = myFirstSplit[0].Split(",");
    string[] mySecondSetSplit = myFirstSplit[1].Split(",");
    
    
    sVal1 = myFirstSetSplit[0].Replace("\r","");
    sVal2 = myFirstSetSplit[1].Replace("\r","");
    
    sVal3 = mySecondSetSplit[0].Replace("\r","");
    sVal4 = mySecondSetSplit[1].Replace("\r","");
    Of course, this is assuming that the string will always be the same format.


    The reason your code isn't working is because of the second time through the outer loop. It resets the iFld back to zero, and overwrites the values that were written the first time.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    india
    Posts
    273

    hellswraith

    ya I got i
    thanks

    regards

    PPCC

    Originally posted by hellswraith
    Why don't you just do this:
    Code:
    columnFields="dww,243\n\r32,sedwe\n\r";
    
    string[] myFirstSplit = columnFields.Split("\n");
    string[] myFirstSetSplit = myFirstSplit[0].Split(",");
    string[] mySecondSetSplit = myFirstSplit[1].Split(",");
    
    
    sVal1 = myFirstSetSplit[0].Replace("\r","");
    sVal2 = myFirstSetSplit[1].Replace("\r","");
    
    sVal3 = mySecondSetSplit[0].Replace("\r","");
    sVal4 = mySecondSetSplit[1].Replace("\r","");
    Of course, this is assuming that the string will always be the same format.


    The reason your code isn't working is because of the second time through the outer loop. It resets the iFld back to zero, and overwrites the values that were written the first time.

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