Splitting a string with more than one commas in it.

Posted on 2005-04-03
Medium Priority
Last Modified: 2010-04-16
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.
Question by:raghuadavi
LVL 21

Accepted Solution

Yurich earned 1000 total points
ID: 13693780
probably i didn't get something, but what's wrong with using simple:

string s = "abc, def,, gfh";  // you string with comma(s)

string[] strAr = s.Split( "," );  // here you'll have an array of strings

LVL 16

Assisted Solution

OliWarner earned 1000 total points
ID: 13694891
yeah if ",," means something magical then do:

string s = "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,"

string[] sArr1 = s.split(",,");

for (int i=0;i<sArr1.length;i++) {
  string[] line = sArr1.split(",");
  for (int j=0;j<line.length;j++) {
    //do whatever you like with the bits

That's by no means neccessary if ",," just means there's a blank entry. But if it represents something special you might want to do it this way.

Expert Comment

ID: 13695667
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      
            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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13695783

You can replace ",," by "," and after that split the resulted string:
string str1 = "..... your string ...";
string str2 = str1.Replace(",,", ",");
string[] words = str2.Split(',');

LVL 17

Expert Comment

by:Jesse Houwing
ID: 13695912
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 */

Expert Comment

ID: 13707475
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
LVL 16

Expert Comment

ID: 13913915
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question