Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: code below groups unique tokens with count</p> <pre><code>string[] target = src.Split(new char[] { ' ' }); var results = target.GroupBy(t =&gt; new { str = t, count = target.Count(sub =&gt; sub.Equals(t)) }); </code></pre> <p>This is finally starting to make more sense to me...</p> <p>EDIT: code below results in count correlated with target substring:</p> <pre><code>string src = "for each character in the string, take the rest of the " + "string starting from that character " + "as a substring; count it if it starts with the target string"; string[] target = {"string", "the", "in"}; var results = target.Select((t, index) =&gt; new {str = t, count = src.Select((c, i) =&gt; src.Substring(i)). Count(sub =&gt; sub.StartsWith(t))}); </code></pre> <p>Results is now:</p> <pre><code>+ [0] { str = "string", count = 4 } &lt;Anonymous Type&gt; + [1] { str = "the", count = 4 } &lt;Anonymous Type&gt; + [2] { str = "in", count = 6 } &lt;Anonymous Type&gt; </code></pre> <p>Original code below:</p> <pre><code>string src = "for each character in the string, take the rest of the " + "string starting from that character " + "as a substring; count it if it starts with the target string"; string[] target = {"string", "the", "in"}; var results = target.Select(t =&gt; src.Select((c, i) =&gt; src.Substring(i)). Count(sub =&gt; sub.StartsWith(t))).OrderByDescending(t =&gt; t); </code></pre> <p>with grateful acknowledgement to <a href="https://stackoverflow.com/questions/541954/how-would-you-count-occurences-of-a-string-within-a-string-c">this previous response</a>.</p> <p>Results from debugger (which need extra logic to include the matching string with its count):</p> <pre><code>- results {System.Linq.OrderedEnumerable&lt;int,int&gt;} - Results View Expanding the Results View will enumerate the IEnumerable [0] 6 int [1] 4 int [2] 4 int </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload