Return to Snippet

Revision: 34873
at October 29, 2010 11:04 by mcbutterbuns


Initial Code
public List<string> splitTags(string tags)
{
    StringBuilder b = new StringBuilder();
    List<string> tagList = new List<string>();
    bool inQuoteState = false;

    foreach (char character in tags.ToCharArray())
    {
        switch (character)
        {
            case '"':
                if (inQuoteState){
                    tagList.Add(b.ToString());
                    b = new StringBuilder();
                }

                inQuoteState = !inQuoteState;
                break;
            case ' ':
                if (!inQuoteState)
                {
                    if (b.Length > 0) //don't add empty tags
                        tagList.Add(b.ToString());

                    b = new StringBuilder();
                }
                else
                    b.Append(character);
                break;
            default:
                b.Append(character);
                break;
        }
    }

    return tagList;
}

Initial URL
http://www.planetjonny.com

Initial Description
This code should split a tag string provided in a format similar to YouTube or Flickr.

Input string:
tag1 tag2 "tag3 tag4"

Output as a list:
tag1
tag2
tag3 tag4

I'm not entirely positive this works. I just wrote it for a coworker but hopefully it will provide someone with a starting point if it doesn't work.

Initial Title
Split tag string

Initial Tags


Initial Language
C#