C# 在字符串列表中查找子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10488587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:02:24  来源:igfitidea点击:

Find substring in a list of strings

c#.netstringlinqsubstring

提问by user1380621

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:

我想"There"在我的列表中搜索并返回"4234 - There"。我试过了:

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

采纳答案by BrokenGlass

With Linq, just retrieving the first result:

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

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 FindAllmethod, which in this case would return all items in the list that contain the search term:

要在没有 Linq 的情况下执行此操作(例如,对于较早的 .NET 版本,例如 .NET 2.0),您可以使用List<T>'sFindAll方法,在这种情况下,它将返回列表中包含搜索词的所有项目:

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

回答by abatishchev

To return all th entries:

返回所有第 th 个条目:

IEnumerable<string> result = list.Where(s => s.Contains(search));

Only the first one:

只有第一个:

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

回答by Conrad Frix

What you've written causes the compile error

您所写的内容导致编译错误

The best overloaded method match for 'string.Substring(int)' has some invalid arguments

'string.Substring(int)' 的最佳重载方法匹配有一些无效参数

Substring is used to get part of string using character position and/or length of the resultant string.

子字符串用于使用字符位置和/或结果字符串的长度来获取字符串的一部分。

for example srch.Substring(1, 3)returns the string "her"

例如 srch.Substring(1, 3)返回字符串“她”

As other have mentioned you should use Containswhich tells you if one string occurs within another. If you wanted to know the actual position you'd use IndexOf

正如其他人提到的,您应该使用Containswhich 告诉您一个字符串是否出现在另一个字符串中。如果你想知道你会使用的实际位置IndexOf

回答by unarity

i like to use indexOf or contains

我喜欢使用 indexOf 或 contains

someString.IndexOf("this");
someString.Contains("this");

回答by a joka

same problem i had to do.

我不得不做同样的问题。

You need this:

你需要这个:

myList.Where(listStrinEntry => myString.IndexOf(listStringEntry) != -1)

Where:

在哪里:

myListis List<String>has the values that myStringhas to contain at any position

myListisList<String>具有myString必须包含在任何位置的值

So de facto you search if myString contains any of the entries from the list.Hope this is what you wanted...

因此,事实上您搜索 myString 是否包含列表中的任何条目。希望这是你想要的......

回答by Diego Venancio

And for CaseSensitiveuse:

CaseSensitive使用:

YourObj yourobj = list.FirstOrDefault(obj => obj.SomeString.ToLower().Contains("some substring"));

OR

或者

YourObj yourobj = list.FirstOrDefault(obj => obj.SomeString.ToUpper().Contains("some substring"));