【发布时间】:2018-08-23 19:43:24
【问题描述】:
鉴于我有以下信息:
string Sentence = "The dog jumped over the cat and the cat jumped above the mouse."
string startword = "jumped"
string endword = "the"
我的要求是如何在 C# 中编程以计算句子包含 startword 开头直到匹配第二个 endword 的出现次数。
上面的例子应该返回 2 因为The dog [jumped] ... [the] cat and ...cat [jumped] .. [the] mouse.
我的一个想法是做字符串。将句子分成单词字符串并循环遍历单词并与startword进行比较。如果startword 匹配,则将下一个单词与endword 进行比较,直到找到或句子结束。如果找到了startword 和endword,则增加计数器,然后继续搜索startword 和endword,直到句子结束。
任何其他建议或代码示例将不胜感激。
【问题讨论】:
-
请把你的想法变成代码。这会有所帮助。然后你可以检查你的确切位置。
标签: c# regex string split console-application