-10

I am trying to split each line from a text file

Each line is something like this:

string text=3,"dac","fsdf,sdf","DdsA 102-13",62.560000000000002,"1397","bes","165/70/R13",945,1380,"Break",10

I need to split it and need to obtain these 12 columns. Is there a way to split it and get 12 columns? as you can see there are parts which contain also "," (comma)

Ekansh Gupta
  • 417
  • 1
  • 5
  • 14
  • So you know that you're lookig for `Split`, have you found anything? This requirement is not particularly groundbreaking. – Tim Schmelter May 27 '14 at 11:28
  • 2
    Possible duplicate http://stackoverflow.com/questions/18228982/splitting-a-string-with-elements-containing-delimiter – Fred May 27 '14 at 11:32

3 Answers3

2
 public static String[] SplitCsv(String value)
    {            if (Object.ReferenceEquals(null, value))
            return null;
        const Char quotation = '\"';
        const Char separator = ',';
        List<String> result = new List<String>();
        Boolean inQuotation = false;
        Boolean isStarted = false;
        StringBuilder Sb = new StringBuilder();
        foreach (Char Ch in value)
        {
            if (inQuotation)
            {
                Sb.Append(Ch);
                inQuotation = Ch != quotation;
                continue;
            }

            if (Ch == separator)
            {
                result.Add(Sb.ToString());
                Sb.Length = 0;
                isStarted = true;
            }
            else
            {
                Sb.Append(Ch);
                isStarted = false;
                inQuotation = Ch == quotation;
            }
        }

        if (isStarted || (Sb.Length > 0))
            result.Add(Sb.ToString());

        return result.ToArray();
    }
Robert P
  • 137
  • 2
  • 10
-1

Try something like this:

string s = "3,dac,fsdfsdf,DdsA 102-13,62.560000000000002,1397,bes,165/70/R13,945,1380,Break,10"
string[] words = s.Split(',');
foreach (string word in words)
{
    Console.WriteLine(word);
}

The string.Split() method will return an array of strings, and via the parameter you can specify the character by which it should split the original string.

Update:

Okay, please see this answer. Slightly modified code from there:

public IEnumerable<string> SplitCSV(string input)
{
    Regex csvSplit = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);

    foreach (Match match in csvSplit.Matches(input))
    {
        yield return match.Value.TrimStart(',');
    }
}

string s = "3,\"dac\",\"fsdf,sdf\",\"DdsA 102-13\",62.560000000000002,\"1397\",\"bes\",\"165/70/R13\",945,1380,\"Break\",10";
var words = SplitCSV(s);
foreach (string word in words)
{
    Console.WriteLine(word); 
}

On how to apply this to all lines in a file:

var lines = System.IO.File.ReadAllLines(@"inputfile.txt");
foreach (var line in lines)
{
    var wordsInLine = SplitCSV(line);
    // do whatever you want with the result...
}
Community
  • 1
  • 1
qqbenq
  • 10,220
  • 4
  • 40
  • 45
  • yes I did this, but in some parts it's possible to be something like "asdasd,asda" and so it will split one more time. – user3679520 May 27 '14 at 11:29
  • That was just a sample line, OP wants all fields of all lines. – Tim Schmelter May 27 '14 at 11:29
  • @TimSchmelter I thought the question was about splitting one line into 12 columns, and actually I still think so :) – qqbenq May 27 '14 at 11:42
  • But OP mentioned that this is just a sample: _"I am trying to split each line from a text file, each line is something like this: ..."_ – Tim Schmelter May 27 '14 at 11:53
  • I really don't like to be the kind of person that I am right now, but if you read just two more sentences: _"Is there a way to split it and get 12 columns?"_ This is the only question in the text :) – qqbenq May 27 '14 at 12:19
-1
string[] lines = System.IO.File.ReadAllLines(@"textfile.txt");
string result[][];

foreach (string line in lines)
{
    string[] splitedLine = line.Split(',');
    result.Add(splitedLine);
}

result is an array with all you splited line (your line could have different number of words)

Then, you can save into a text file or do what you want

angel
  • 322
  • 1
  • 7