我正在尝试获取字符串列表(邮政编码)中的下一项。通常情况下,我会一直到找到它,然后在列表中找到下一个,但我试图让它更直观、更紧凑(更像是一种练习,而不是任何东西)。
我可以使用lambda很容易地找到它:
List<string> postalCodes = new List<string> { "A1B", "A2B", "A3B" };
currentPostalCode = "A2B";
postalCodes.Find((s) => s == currentPostalCode);
这很酷,而且我正确地得到了"A2B",但我更喜欢索引而不是值。
发布于 2010-05-11 17:33:31
您可以使用IndexOf
方法(这是泛型List<T>
类的标准方法):
List<string> postalCodes = new List<string> { "A1B", "A2B", "A3B" };
currentPostalCode = "A2B";
int index = postalCodes.IndexOf(currentPostalCode);
有关详细信息,请参阅MSDN。
发布于 2010-05-11 17:35:22
只需从查找切换(...)到FindIndex(...)
List<string> postalCodes = new List<string> { "A1B", "A2B", "A3B" };
currentPostalCode = "A2B";
postalCodes.FindIndex(s => s == currentPostalCode);
发布于 2010-05-11 17:35:39
试试这个:
int indexofA2B = postalCodes.IndexOf("A2B");
https://stackoverflow.com/questions/2812975
复制相似问题