2012-03-19 52 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) 
     { 
     } 
相關問題