/ Published in: C#
URL: http://www.planetjonny.com
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.
Expand |
Embed | Plain Text
public List<string> splitTags(string tags) { bool inQuoteState = false; foreach (char character in tags.ToCharArray()) { switch (character) { case '"': if (inQuoteState){ tagList.Add(b.ToString()); } inQuoteState = !inQuoteState; break; case ' ': if (!inQuoteState) { if (b.Length > 0) //don't add empty tags tagList.Add(b.ToString()); } else b.Append(character); break; default: b.Append(character); break; } } return tagList; }
Comments
Subscribe to comments
You need to login to post a comment.

The code doesn´t save the last tag.
put a
if(b.Length > 0) tagList.Add(b.ToString());before the return;