c# - Word count by LINQ -


I need to calculate the word by LINQ. I here is the code that I use to count the words in long string array I am, but it is not very efficient:

  public stable int WordCount (string thunder, string needle) {if (needle == blank) {return 0; } String [] source = haystack.Split (new char [] {'.', '?', '!', '', ';', ':', ',', '*', '-'} , StringSplit option. RemoveEmptyEntries); Var matchQuery = words where word is in the source ToLowerInvariant () == Needle ToLowerInvariant () Select the word; Int count = matchQuery.Count (); Calculation of return; }   

Suppose I have a string like this:

  Geo Prism Geo 1995 Geo * - ABS # 16213899 HGOO-   

If I try to find a GEO in the above sentence, then my routine is not coming back in the correct number: I hope 4 What's wrong with my routine?

You can get it as a liner with LINQ:

 <0> Zero Main () {String Data = "Geo Prism Geo 1995 GO * - ABS # 16213899 HGIO"; Var target = "geo"; Count = data Select ((C, I) => Data.Substring (I)). Calculation (sub = & gt; sub-formula). Start (goal); Console.WriteLine (count.ToString ()); }   

Result:

  4    

Comments