C# 如何使用LINQ在字符串列表中查找子字符串
我需要使用它的子字符串从字符串集合中找出一个字符串。这个子字符串C# 如何使用LINQ在字符串列表中查找子字符串,c#,linq,C#,Linq,我需要使用它的子字符串从字符串集合中找出一个字符串。这个子字符串 一定是在起步阶段 你可以这样做 collection.FirstOrDefault(s => s.StartsWith(whatever)) List<string> collection = new List<string>(); collection.Add("example sample"); collection.Add("sample");
一定是在起步阶段 你可以这样做
collection.FirstOrDefault(s => s.StartsWith(whatever))
List<string> collection = new List<string>();
collection.Add("example sample");
collection.Add("sample");
collection.Add("example");
var varSubstring = collection.Where(x => x.IndexOf("sample")==0).ToList();
foreach (var vartemp in varSubstring)
{
}
列表集合=新列表();
收集。添加(“示例样本”);
收集。添加(“样本”);
集合。添加(“示例”);
var varSubstring=collection.Where(x=>x.IndexOf(“sample”)==0.ToList();
foreach(varSubstring中的vartemp变量)
{
}
您可以这样做
List<string> collection = new List<string>();
collection.Add("example sample");
collection.Add("sample");
collection.Add("example");
var varSubstring = collection.Where(x => x.IndexOf("sample")==0).ToList();
foreach (var vartemp in varSubstring)
{
}
列表集合=新列表();
收集。添加(“示例样本”);
收集。添加(“样本”);
集合。添加(“示例”);
var varSubstring=collection.Where(x=>x.IndexOf(“sample”)==0.ToList();
foreach(varSubstring中的vartemp变量)
{
}
或者,如果可能有多个,则使用collection。其中(s=>s.StartsWith(无论什么))
或者,如果可能有多个,则使用collection。其中(s=>s.StartsWith(无论什么))
尝试这个和这个