【发布时间】:2015-07-14 20:43:46
【问题描述】:
我有一个正在使用 C# 进行的项目。我有两个字符数组。一个是句子,一个是单词。我必须遍历句子数组,直到找到一个与变成单词数组的单词相匹配的单词通过与单词数组相同的长度找到单词?
代码:
String wordString = "(Four)";
String sentenceString = "Maybe the fowl of Uruguay repeaters (Four) will be found";
char[] wordArray = wordString.ToCharArray();
List<String> words = sentenceString.Split(' ').ToList<string>();
//This would be the part where I iterate through sentence
foreach (string sentence in sentArray)
{
//Here I would need to find where the string of (Four) and trim it and see if it equals the wordString.
if (sentence.Contains(wordString)
{
//At this point I would need to go back the length of wordString which happens to be four places but I'm not sure how to do this. And for each word I go back in the sentence I need to capture that in another string array.
}
我不知道我对此是否足够清楚,但如果我不是,请随时询问.. 提前谢谢你。这应该返回的是“乌拉圭中继器的家禽”。所以基本上用例是括号中的字母数,逻辑应该在括号中的单词之前返回相同数量的单词。
【问题讨论】:
-
你必须只使用char数组??究竟输出应该是什么?
-
不,这不应该是我想用的,不知道这是否是最好的方法。这个输出应该返回“乌拉圭中继器的家禽”
-
是什么阻止你使用 sentenceString.IndexOf(wordString) ?
-
使用 indexOf、Substring 等的组合
-
为什么输出应该返回“乌拉圭中继器的家禽”而不是“乌拉圭中继器的家禽”?