Skip to main content
 首页 » 编程设计

c#之我如何逐字搜索(或获取)句子

2024年02月23日16dflying

我有一个词和文本。我必须找到所有包含该词的提案。 你有什么想法吗?

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(); 
} 

最后,匹配项将是一个枚举,其中包含包含您要搜索的词的所有句子。