PDA

Click to See Complete Forum and Search --> : How to split the string in simple way


PPCC
Aug 19th, 2003, 01:35 AM
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

hellswraith
Aug 19th, 2003, 12:25 PM
Why don't you just do this:

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.

PPCC
Aug 21st, 2003, 04:36 AM
ya I got i
thanks

regards

PPCC

Originally posted by hellswraith
Why don't you just do this:

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.