寻找在子字符串列表 [英] Find substring in a list of strings

查看:146
本文介绍了寻找在子字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像这样的列表,我希望能够在这个列表中搜索字符串从另一字符串的到来。例如:

 名单,其中,串>名单=新的名单,其中,串>();
字符串SRCH =有;
list.Add(1234  - 你好);
list.Add(4234  - 有);
list.Add(2342  - 世界);
 

我要搜索有在我的名单,并返回4234 - 那里。我尝试过:

  VAR MYSEARCH = list.FindAll(S => s.substring(查找));
的foreach(在MYSEARCH VAR温度)
{
    字符串结果=温度;
}
 

解决方案

使用LINQ,只需检索的第一个结果:

 字符串结果= list.FirstOrDefault(S => s.Contains(查找));
 

要做到这一点W / O型的LINQ(如早期的.NET版本,如.NET 2.0),您可以使用名单,其中,T> 的FindAll 方法,在这种情况下将返回所有项目列表中包含搜索词:

  VAR resultList = list.FindAll(委托(字符串s){返回s.Contains(查找);});
 

I have a list like so and I want to be able to search within this list for a substring coming from another string. Example:

List<string> list = new List<string>();
string srch = "There";
list.Add("1234 - Hello");
list.Add("4234 - There");
list.Add("2342 - World");

I want to search for "There" within my list and return "4234 - There". I've tried:

var mySearch = list.FindAll(S => s.substring(srch));
foreach(var temp in mySearch)
{
    string result = temp;
}

解决方案

With Linq, just retrieving the first result:

string result = list.FirstOrDefault(s => s.Contains(srch));

To do this w/o Linq (e.g. for earlier .NET version such as .NET 2.0) you can use List<T>'s FindAll method, which in this case would return all items in the list that contain the search term:

var resultList = list.FindAll(delegate(string s) { return s.Contains(srch); });

这篇关于寻找在子字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆