2012-03-19 50 views
0

我需要从使用其子字符串的字符串集合中找出一个字符串。该子字符串 必须处于启动状态。如何使用LINQ找到字符串列表中的子串

+0

试试这个http://stackoverflow.com/questions/2402529/substring-with-linq这http://stackoverflow.com/questions/418187/how-to-use-linq-to-return-substring-of-fileinfo-name – PresleyDias 2012-03-19 05:08:25

回答

9
collection.FirstOrDefault(s => s.StartsWith(whatever)) 
+0

谢谢你的工作很好 – YogeshWaran 2012-03-19 05:08:02

+2

或者,如果可能有多个,使用'collection.Where(s => s。 StartsWith(whatever))' – joshuahealy 2012-03-19 05:08:11

+0

@appclay:谢谢 – YogeshWaran 2012-03-19 05:09:49

2

你可以做这样的事情,

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) 
     { 
     } 
相关问题