/ Published in: C#

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.
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
Copy this code and paste it in your HTML
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; }
URL: http://www.planetjonny.com
Comments
