試圖找出如何檢查下一個字符串與當前字符串在一個循環(僞代碼):檢查下一個字符串
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
它應該在列表中工作。更好的方法是使用先前的值和第一個元素的單獨大小來檢查當前值,以便在重複名稱到達時執行邏輯。 –