如何查找属于字符串的List的匹配项。 [英] How to find the matching item of List which belongs to a string.
问题描述
我有一个名为FunctionList的项目列表,还有一个名为mainString的字符串?
我想找到来自属于mainString的FunctionList的匹配项目。
输出将是
var matchItem = 检查每个项目到字符串是否属于字符串跨度>;
这里matchItem也是List属于字符串的项目。请帮我用C#或LINQ找出这个解决方案的代码。
好的 - 我认为我知道你是什么询问
你有一个List< item> FunctionList和\s分隔的术语串。
您想为字符串中的每个术语找到匹配的FunctionList项。
我想,FunctionList的某些部分是一个与术语匹配的字符串?
如果这不正确那么请评论我将更新或删除我的答案!
// 首先,将您的条款分成数组
string [] terms = mainString.split( ' '); // 如果你的mainString有脏数据,你可以在之前删除双空格或解析空字符串
foreach ( string term in terms)
{
// 如果FunctionList是List< string> ;
if (functionList.Contains(term)){}
// 但我怀疑它是
// 使用linq:
var items = functionList.Where(f => f.Name == term ); // 猜测有一个字符串字段 - 称之为名字
if (!items.Any()){ continue ; // 无匹配}
其他 if (items.Count()> 1){ break ; // 多个匹配}
其他 {
// 完全匹配1个
项目项目=项目。< span class =code-sdkkeyword> Single ();
item.DoSomething();
}
// 使用第二个循环
foreach (项目 in FunctionList){
if (item.Name = term)
{
item.DoSomething();
// 首次匹配后退出此循环?
断裂跨度>;
}
// 如果需要,您可以处理多个/不匹配问题
// 这只是一个基本的例子
}
}
我不确定我是否理解你,但是......如果你想找到比赛,试试这个:
List< string> FunctionList = new List< string>(){ bla , ble, bli, blo};
string mainString = bli跨度>;
var matches = FunctionList.Where(a => a == mainString);
// 或
/ / var matches = FunctionList.Where(a => a.Contains(mainString));
foreach ( var m 匹配)
{
Console.WriteLine( {0},m);
}
如需了解更多信息,请参阅:
Enumerable.Contains<tsource>方法(IEnumerable< tsource>,TSource) [ ^ ]
101 LINQ样本 [ ^
I have a List of Item named "FunctionList" and also have a string named "mainString"?
I want to find the matching item one by one from "FunctionList" which belongs to "mainString".
The output will be like
var matchItem= "check each item into string belongs to string or not";
Here matchItem will be the item from List belongs to string also. Please help me to find out the code for this solution using C# or LINQ.
Ok - I think I get what you're asking
You have a List<item> FunctionList and a \s separated string of terms.
You want to find the matching FunctionList item for each term in the string.
I imagine that some part of the FunctionList is a string that matches the term?
If this is not correct then please comment and I will update or remove my answer!
//first, break your terms into an array string[] terms = mainString.split(' '); // you can get rid of double spaces before or parse for empty strings after if your mainString has dirty data foreach(string term in terms) { //if the FunctionList is a List<string> if(functionList.Contains(term)){} //but I doubt that it is //using linq: var items = functionList.Where(f=>f.Name==term); //guessing there's a string field - calling it name if(!items.Any()){continue; // No Match} else if(items.Count()>1) {break; //multiple matches} else{ //Exactly 1 match Item item = items.Single(); item.DoSomething(); } //using a second loop foreach(Item item in FunctionList){ if(item.Name = term) { item.DoSomething(); //exit this loop after first match? break; } //You can handle multiple / no match issues if you need to //This is just a basic example } }
I'm not sure i understand you well, but... if you want to find matches, try this:
List<string> FunctionList = new List<string>(){"bla","ble","bli","blo"}; string mainString = "bli"; var matches = FunctionList.Where(a=>a==mainString); //or //var matches = FunctionList.Where(a=>a.Contains(mainString)); foreach(var m in matches) { Console.WriteLine("{0}", m); }
For further information, please see:
Enumerable.Contains<tsource> Method (IEnumerable<tsource>, TSource)[^]
101 LINQ Samples[^]
这篇关于如何查找属于字符串的List的匹配项。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!