Link to home
Start Free TrialLog in
Avatar of raghuadavi
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,200504032100,O,0,00,04/04/2005 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.
ASKER CERTIFIED SOLUTION
Avatar of Yurich
Yurich
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Basically what you have to do to handle the  " ,," and many number of commas together:

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
      
Avatar of gena17
gena17

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
Avatar of Jesse Houwing
You could do a

System.Text.RegularExpressions.Match(inputstring, "(?:,|^)(?<value>[^,]*)", new System.Text.RegularExpressions.MatchEvaluator(this.Function), System.Text.RegularExpressions.RegexOptions.ExplicitCapture);

and declare a function like this:

public string Function(System.Text.RegularExpression.Match m)
{
     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(delimiter);  
                  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.