raghuadavi
asked on
Splitting a string with more than one commas in it.
I have got a string as follows:
04/03/2005 21:31:51,,412888,200504032 100,O,0,00 ,04/04/200 5 08:00, 35.43,,00,04/05/2005 08:00, 38.16,,00,04/06/2005 08:00, 47.26,,00,04/07/2005 08:00, 54.36,,00,04/08/2005 08:00, 49.17,,00,04/09/2005 08:00, 45.39,,00,04/10/2005 08:00, 43.23,,Done,
I want this string to be splitted with comma(s) as the delimiter and return me the individual strings. I need these strings for passing them to a function.
I want this be done only in C#. I have some constraints on the language used.
Please anyone help me with this ASAP, as this and urgent requirement for one of my projects I am working one.
Thanks in advance for your help.
04/03/2005 21:31:51,,412888,200504032
I want this string to be splitted with comma(s) as the delimiter and return me the individual strings. I need these strings for passing them to a function.
I want this be done only in C#. I have some constraints on the language used.
Please anyone help me with this ASAP, as this and urgent requirement for one of my projects I am working one.
Thanks in advance for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
You can replace ",," by "," and after that split the resulted string:
string str1 = "..... your string ...";
string str2 = str1.Replace(",,", ",");
string[] words = str2.Split(',');
Gena
You can replace ",," by "," and after that split the resulted string:
string str1 = "..... your string ...";
string str2 = str1.Replace(",,", ",");
string[] words = str2.Split(',');
Gena
You could do a
System.Text.RegularExpress ions.Match (inputstri ng, "(?:,|^)(?<value>[^,]*)", new System.Text.RegularExpress ions.Match Evaluator( this.Funct ion), System.Text.RegularExpress ions.Regex Options.Ex plicitCapt ure);
and declare a function like this:
public string Function(System.Text.Regul arExpressi on.Match m)
{
string textBetweenComma = m.Groups["value"].Value;
if (textBetweenComma.Length != 0)
{
/* Do your stuff */
}
}
System.Text.RegularExpress
and declare a function like this:
public string Function(System.Text.Regul
{
string textBetweenComma = m.Groups["value"].Value;
if (textBetweenComma.Length != 0)
{
/* Do your stuff */
}
}
you could have a function like this which splits the string given the delimiter
private string[] splitString(string parentString,string delimStr)
{
string[] split = null;
char [] delimiter = delimStr.ToCharArray();
split = parentString.Split(delimit er);
return split;
}
It will return an array and you have access the elements in the array to get all your strings
private string[] splitString(string parentString,string delimStr)
{
string[] split = null;
char [] delimiter = delimStr.ToCharArray();
split = parentString.Split(delimit
return split;
}
It will return an array and you have access the elements in the array to get all your strings
Yurich and I had the 2 first code segments, one working on the assumption ",," was something magical (mine) and Yurich's not...
As there was no feedback from raghuadavi its not really possible to say which is more correct...
... either way... this question is more than answered.
As there was no feedback from raghuadavi its not really possible to say which is more correct...
... either way... this question is more than answered.
immediately reaching a "," do the necessary operation increment the string to count 1 and check whether it is a ","
IF it a comma then add to the array what ever you want eg: empty string and increment the string again and check for ,
If it is not a comma decrement the string and go to the normal loop to check the string again
loop A check string for ","
If found
{
add it to the array
loop c increment the string
check for the ","
if found add to the array with empty string and increment string and check for the , again
else
decrement string by 1
exit from loop C
end loop c
loop A
This is a scratch and you have to build it from this.
sun4sunday