list集合中如何查找匹配的索引。

frdking 2013-12-04 03:16:41
例如:
List<string> ss = new List<string>();
ss.add("用户127.0.0.1:1000");
ss.add("用户127.0.0.1:1001");
ss.add("用户127.0.0.1:1002");

我要找到 127.0.0.1:1001 包含这个字符串的list索引
indexof 方法只能匹配 "用户127.0.0.1:1001" 这样的查找方式

...全文
5472 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2013-12-04
  • 打赏
  • 举报
回复
var query = ss.Select((x, i) => new { x, i }).Where(x => x.x == "127.0.0.1:1000").Select(x => x.i); foreach (var item in query) Console.WriteLine(item);
wind_cloud2011 2013-12-04
  • 打赏
  • 举报
回复
 List<string> item1 = new List<string>();
            item1.Add("aaa");
            item1.Add("bbb");
            item1.Add("用户127.0.0.1:1000");
            item1.Add("用户127.0.0.1:1001");
            item1.Add("用户127.0.0.1:1002");
            string item = item1.First(str => str.Contains("127.0.0.1:1000"));            
item ="用户127.0.0.1:1000"
你的选择H 2013-12-04
  • 打赏
  • 举报
回复

int index = ss.FindIndex(c => c.Contains("127.0.0.1:1001"));
frdking 2013-12-04
  • 打赏
  • 举报
回复
引用 1 楼 guwei4037 的回复:
int index = ss.FindIndex(x => x == "用户127.0.0.1:1001");
这个我用indexof 也能得到。我意思是只要匹配127.0.0.1:1001这样的关键字的集合索引。 foreach (string s in ss) { bool b = ss.Contains("127.0.0.1:1001"); if (b) { userList = s; } } int n = ss.IndexOf(userList); 我现在这能用这个方法可以得到索引。
wg5945 2013-12-04
  • 打赏
  • 举报
回复
Contains
全栈极简 2013-12-04
  • 打赏
  • 举报
回复
int index = ss.FindIndex(x => x == "用户127.0.0.1:1001");

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧