?
Solved

Splitting a string with more than one commas in it.

Posted on 2005-04-03
10
Medium Priority
?
255 Views
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.
0
Comment
Question by:raghuadavi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 21

Accepted Solution

by:
Yurich earned 1000 total points
ID: 13693780
hello,
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


     
0
 
LVL 16

Assisted Solution

by:OliWarner
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.
0
 
LVL 9

Expert Comment

by:sun4sunday
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      
        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
      
0
Independent Software Vendors: 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!

 
LVL 8

Expert Comment

by:gena17
ID: 13695783
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
0
 
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 */
     }
}
0
 

Expert Comment

by:sendhil_an
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
0
 
LVL 16

Expert Comment

by:OliWarner
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.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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