试图找出如何检查下一个字符串与当前字符串在一个循环(伪代码):检查下一个字符串
string currentName = string.Empty;
for(int i=0; i < SomeList.Count; i++)
{
currentName = SomeList[i].Name;
//do some other logic here
if(string.Compare(SomeList[i].Name, SomeList[i+1].Name) == 0)
// do something
}
这似乎并没有工作:
if(string.Compare(SomeList[i].Name, SomeList[i+1].Name)
我想看看当前字符串是否与循环中下一个迭代之前循环中的下一个字符串相同。
1)SomeList [i] .Name == SomeList [i + 1] .Name'有什么问题? 2)如何处理最后一个元素? – NullUserException
它不起作用? – BoltClock
它应该在列表中工作。更好的方法是使用先前的值和第一个元素的单独大小来检查当前值,以便在重复名称到达时执行逻辑。 –