|
-
Aug 19th, 2003, 01:35 AM
#1
Thread Starter
Hyperactive Member
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
-
Aug 19th, 2003, 12:25 PM
#2
PowerPoster
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.
-
Aug 21st, 2003, 04:36 AM
#3
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|