Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3237476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:44:35+00:00 2026-05-17T17:44:35+00:00

Curious if this can be simplified… internal static IEnumerable<string> Split(string str, char sep =

  • 0

Curious if this can be simplified…

internal static IEnumerable<string> Split(string str, char sep = ',')
{
    int lastIndex = 0;
    bool quoted = false;
    bool escaped = false;
    bool bracketed = false;
    char lastQuote = '\0';

    for (int i = 0; i < str.Length; ++i)
    {
        if (str[i] == '[')
        {
            if (!quoted && !escaped)
                bracketed = true;
            escaped = false;
        }
        else if (str[i] == ']')
        {
            if (!quoted && !escaped)
                bracketed = false;
            escaped = false;
        }
        else if (str[i] == '\\')
        {
            escaped = !escaped;
        }
        else if (str[i] == '"' || str[i] == '\'')
        {
            if (!escaped)
            {
                if (quoted)
                {
                    if (lastQuote == str[i])
                        quoted = false;
                }
                else
                {
                    quoted = true;
                    lastQuote = str[i];
                }
            }
            escaped = false;
        }
        else if (str[i] == sep)
        {
            if (!quoted && !escaped && !bracketed)
            {
                yield return str.Substring(lastIndex, i - lastIndex);
                lastIndex = i + 1;
            }
            escaped = false;
        }
        else
        {
            escaped = false;
        }
    }

    yield return str.Substring(lastIndex);
}

Wrote this method to split on commas that aren’t inside [], are not quoted, and are not escaped. Is this inherently a tricky problem, or did I take a dumb approach?

Input:

foreach(var sel in SharpQuery.SplitCommas("\"comma, in quotes\", comma[in,brackets], comma[in \"quotes, and brackets\"], \"woah, 'nelly,' \\\"now you,re [talking, crazy\\\"\"")) {
    Console.WriteLine(sel);
}

Expected output:

"comma, in quotes"
 comma[in,brackets]
 comma[in "quotes, and brackets"]
 "woah, 'nelly,' \"now you,re [talking, crazy\""
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:44:36+00:00Added an answer on May 17, 2026 at 5:44 pm

    A bit of an awkward choice to keep your automaton state. I would use a single variable or a stack in this case. So your current state is always stateStack.Peek(). Easy to read. Easy to handle nested states.

    edit: heres a quick sample. I’m sure you can expand it to add error handling and specifics of your rules.

        enum ParserState
        {
            Text,
            Bracketed,
            Quoted,
            EscapChar,
        }
    
        internal static IEnumerable<string> Split(string str, char sep)
        {
            int lastIdx = 0;
            char c;
            ParserState s;
            Stack<ParserState> state = new Stack<ParserState>();
            state.Push(ParserState.Text);
    
            for (int i = 0; i < str.Length; i++)
            {
                c = str[i];
                s = state.Peek();
    
                if (s == ParserState.EscapChar
                    || (s == ParserState.Bracketed && c == ']')
                    || (s == ParserState.Quoted && c == '"'))
                {
                    state.Pop();
                }
                else if (c == '[')
                    state.Push(ParserState.Bracketed);
                else if (c == '"')
                    state.Push(ParserState.Quoted);
                else if (c == '\\')
                    state.Push(ParserState.EscapChar);
                else if (s == ParserState.Text && c == sep)
                {
                    yield return str.Substring(lastIdx, i - lastIdx);
                    lastIdx = i + 1;
                }
            }
            yield return str.Substring(lastIdx);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this would violate convention, but I'm curious to know if you can
I'm quite curious about this. In a broad way, how does one go about
I'm just curious about this. It strikes me that the behavior of a StringBuilder
I'm just curious about this really, does anyone know why they broke convention on
I found this web site (photoblog) http://www.OneReaction.net/ and I am very curious how this
Our situation is as follows, but I'm curious about this problem in any situation.
I got a little curious after reading this /. article over hijacking HTTPS cookies.
This is mostly a theoretical question I'm just very curious about. (I'm not trying
I read this answer and its comments and I'm curious: Are there any reasons
This is something I'm not much consistent about and always curious about what other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.