如何按单词搜索(或获取)句子

本文关键字:获取 句子 何按单 搜索 | 更新日期: 2023-09-27 18:16:01

我有一个单词和文本。我得找到所有有这个词的提案。你知道吗?

piblic List<string> GetSnetences(string word)
{
    // search all proposals that have the word
{

如何按单词搜索(或获取)句子

试着这样做:

var text =
    "Here is some text. It has some sentences. There are a few sentences.";
var word = "SOME";
public List<String> GetSentences(String text, String word) {
    var sentences =
        text.Split(new[] { ". " }, StringSplitOptions.RemoveEmptyEntries);
    var matches = from sentence in sentences
                  where sentence.ToLower().Contains(word.ToLower())
                  select sentence;
    return matches.ToList();
}

最后,匹配将是一个枚举,它包含包含您正在搜索的单词的所有句子。

piblic List<string> GetSnetences(string word)
{
   string text = "Here is some text. It has some sentences. There are a few sentences.";
   List<string> results = text.Split(".");
   return results.FindAll(delegate(string str) { return str.ToLower() == word.ToLower(); });
}

你可以试试这个。

您可以使用字符串的contains()方法来查找字符串是否包含某些字母或单词