String split?
-
Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.
-
Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.
SRKSHOME wrote:
edate=3,,frq
The thing is you have two ,'s here. Based on this
string[2]
will be "".The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
modified on Thursday, October 21, 2010 2:29 PM
-
Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.
-
SRKSHOME wrote:
edate=3,,frq
The thing is you have two ,'s here. Based on this
string[2]
will be "".The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
modified on Thursday, October 21, 2010 2:29 PM
-
Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.
A simple Split will work for every ',' character into the original string, and it seems that it should ignore the ones which are between parenthesis. You can use regular expressions to achieve this. First of all, replace every ',' character which is between parenthesis with another character easy to replace back when split is finished ('|' could be a good choice). Then make a normal split, and then replace back. This code would work for the sample given:
string originalString = "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd";
Regex r=new Regex("\\(.+\\)");string modified = r.Replace(originalString, m =>
{
return m.Value.Replace(',', '|');
});string[] vector = modified.Split(',');
for (int i = 0; i < vector.Length; i++)
vector[i] = vector[i].Replace('|', ',');Though vector[3] would be an empty string due to the two consecutive commas after the edate=3. If you want to ignore several consecutive commas into the string, you should make this before the split and after the replacement made by the Regex object:
while (modified.Contains(",,"))
modified = modified.Replace(",,", ",");Hope this helps. See you
-
A simple Split will work for every ',' character into the original string, and it seems that it should ignore the ones which are between parenthesis. You can use regular expressions to achieve this. First of all, replace every ',' character which is between parenthesis with another character easy to replace back when split is finished ('|' could be a good choice). Then make a normal split, and then replace back. This code would work for the sample given:
string originalString = "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd";
Regex r=new Regex("\\(.+\\)");string modified = r.Replace(originalString, m =>
{
return m.Value.Replace(',', '|');
});string[] vector = modified.Split(',');
for (int i = 0; i < vector.Length; i++)
vector[i] = vector[i].Replace('|', ',');Though vector[3] would be an empty string due to the two consecutive commas after the edate=3. If you want to ignore several consecutive commas into the string, you should make this before the split and after the replacement made by the Regex object:
while (modified.Contains(",,"))
modified = modified.Replace(",,", ",");Hope this helps. See you