积极答复者
如何在数组中查找 包含的内容举例string[] str = { "art", "bti", "cip","lifr","ui","pu","eu","cgh","sloq", }; 查找 u 要最快的方法谢谢

问题
-
举例string[] str = { "art", "bti", "cip","lifr","ui","pu","eu","cgh","sloq", };
数组中查找 a="u"; 的值 如果找到 就返回 含 U 的所有 数值 为 ui pu 如果没找到 返回 "未找到"
- 已移动 Jack Zhai-MSFTMicrosoft contingent staff 2016年12月14日 1:59
答案
-
string[] FindMatchList(string[] list, string str)
{
var matchList = list.ToList().Where(x => x.IndexOf(str) >= 0).ToArray();
if (matchList == null || matchList.Length == 0) throw new Exception("未找到");
return matchList;
}是不是最快的不清楚,不过基本就这样了
- 已标记为答案 ning050089 2016年12月15日 3:08
-
Hi ning050089,
我测试 上面这个代码
var list = str.Where(m => m.Contains("u")).ToList();
是完全可以实现你的要求,而且判断循环已是最少,应该是比较快的。
如果你的问题被解决了,请及时标记有用的回复作为答案。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已标记为答案 ning050089 2016年12月15日 3:07
全部回复
-
string[] FindMatchList(string[] list, string str)
{
var matchList = list.ToList().Where(x => x.IndexOf(str) >= 0).ToArray();
if (matchList == null || matchList.Length == 0) throw new Exception("未找到");
return matchList;
}是不是最快的不清楚,不过基本就这样了
- 已标记为答案 ning050089 2016年12月15日 3:08
-
因为和C#开发相关,我帮你移到更合适的语言开发论坛。
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
Hi ning050089,
我测试 上面这个代码
var list = str.Where(m => m.Contains("u")).ToList();
是完全可以实现你的要求,而且判断循环已是最少,应该是比较快的。
如果你的问题被解决了,请及时标记有用的回复作为答案。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已标记为答案 ning050089 2016年12月15日 3:07