0

I have a text

Category2,"Something with ,comma"

when I split this by ',' it should give me two string

  • Category2
  • "Something with ,comma"

but in actual it split string from every comma.

how can I achieve my expected result.

Thanx

BreakHead
  • 10,480
  • 36
  • 112
  • 165
  • 2
    Break down your problem into a simple set of rules. What do you want to do? Are you saying you want to split on a comma, but *not* if it is enclosed in double quotes? – Rob Levine Jun 14 '11 at 14:06

5 Answers5

4

Just call variable.Split(new char[] { ',' }, 2). Complete documentation in MSDN.

Ahe
  • 2,124
  • 3
  • 19
  • 29
  • Hi thanx, as the solution shows it separate the string from first ',' it works for me, but suppose there is a string My, Name ,Is BreakHead. and it should split like this My, Name and Is BreakHead ? – BreakHead Jun 14 '11 at 14:34
  • General solution depends on your data and what it can contain. Check other answers provided to your question, they also provide nice solutions. – Ahe Jun 15 '11 at 05:53
2

There are a number of things that you could be wanting to do here so I will address a few:

Split on the first comma

String text = text.Split(new char[] { ',' }, 2);

Split on every comma

String text = text.Split(new char[] {','});

Split on a comma not in "

var result = Regex.Split(samplestring, ",(?=(?:[^']*'[^']*')*[^']*$)"); 

Last one taken from C# Regex Split

Community
  • 1
  • 1
msarchet
  • 15,104
  • 2
  • 43
  • 66
  • Your last example works for strings inside ' not inside ". You would have to to this as : `Regex.Split(text, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")` if the string is within ", as in the example of the OP. – Vladimir Jun 14 '11 at 14:29
1

Specify the maximum number of strings you want in the array:

string[] parts = text.Split(new char[] { ',' }, 2);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

String.Split works at the simplest, fastest level - so it splits the text on all of the delimiters you pass into it, and it has no concept of special rules like double-quotes.

If you need a CSV parser which understands double-quotes, then you can write your own or there are some excellent open source parsers available - e.g. http://www.codeproject.com/KB/database/CsvReader.aspx - this is one I've used in several projects and recommend.

Stuart
  • 66,722
  • 7
  • 114
  • 165
0

Try this:

public static class StringExtensions
{
    public static IEnumerable<string> SplitToSubstrings(this string str)
    {
        int startIndex = 0;
        bool isInQuotes = false;

        for (int index = 0; index < str.Length; index++ )
        {
            if (str[index] == '\"')
                isInQuotes = !isInQuotes;

            bool isStartOfNewSubstring = (!isInQuotes && str[index] == ',');                

            if (isStartOfNewSubstring)
            {
                yield return str.Substring(startIndex, index - startIndex).Trim();
                startIndex = index + 1;
            }
        }

        yield return str.Substring(startIndex).Trim();
    }
}

Usage is pretty simple:

foreach(var str in text.SplitToSubstrings())
    Console.WriteLine(str);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459