Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    // Parse tags string.
    // Example:
    // "tag1, tag2" => {"tag1", "tag2"}
    // "tag1, very long tag" => {"tag1", "very long"}
    string[] NewTags(string tags_string)
    {
        //Allow only letters, numbers and spaces in tag.
        tags_string = new string(tags_string.Where(c => char.IsLetter(c)
            || char.IsNumber(c) || c == ' ' || c == ',').ToArray());

        //Separate multiple tags with commas.
        string[] tags = tags_string.Split(',').Select(s => s.Trim()).ToArray();

        //Two word per tag maximum (extra words in a tag will be removed).
        for (int i = 0; i < tags.Length; i++)
        {
            List<string> validWords = new List<string>();
            string[] wordsInTag = tags[i].Split(' ');
            if (wordsInTag.Length > 2)
            {
                tags[i] = wordsInTag[0] + " " + wordsInTag[1];
            }
        }
        return tags;
    }


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: