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

1
  • 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? Commented Jun 14, 2011 at 14:06

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
General solution depends on your data and what it can contain. Check other answers provided to your question, they also provide nice solutions.
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

1 Comment

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.
1

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

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

Comments

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.

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.