在字符串中单词的每一侧获取x个唯一单词?

时间:2010-08-04 22:34:58

标签: c# string

我正在尝试在C#中的字符串中的单词的每一侧获取x个唯一单词。例如,方法

GetUniqueWords("she sells seashells, by the seashore. the shells she sells, are surely seashells.", "seashore", 3) 

(第一个参数是句子字符串。第二个参数是用于获取双方单词的单词。第三个参数是要检查的单词数量)

将返回一个包含值的字符串列表:

海贝
通过

炮弹

提前致谢。

1 个答案:

答案 0 :(得分:2)

不漂亮,但为你的样本工作: - )

    private static IEnumerable<string> GetUniqueWords(string phrase, string word, int amount)
    {
        //Clean up the string and get the words
        string[] words = Regex.Split(phrase.Replace(".","").Replace(",",""),@"\s+");

        //Find the first occurrence of the desired word (spec allows)
        int index = Array.IndexOf(words,word);

        //We don't wrap around the edges if the word is the first, 
        //we won't look before if last, we won't look after, again, spec allows
        int min = (index - amount < 0) ? 0 : index - amount;
        int max = (index + amount > words.Count() - 1) ? words.Count() - 1 : index + amount;

        //Add all the words to a list except the supplied one
        List<string> rv = new List<string>();
        for (int i = min; i <= max; i++)
        {
            if (i == index) continue;
            rv.Add(words[i]);
        }

        //Unique-ify the resulting list
        return rv.Distinct();
    }
}