title: How would you count occurrences of a string (actually a char) within a string?
body: <p>I am doing something where I realised I wanted to count how many <code>/</code>s I could find in a string, and then it struck me, that there were several ways to do it, but couldn't decide on what the best (or easiest) was.</p>
  
  <p>At the moment I'm going with something like:</p>
  
  <pre><code>string source = "/once/upon/a/time/";
  int count = source.Length - source.Replace("/", "").Length;
  </code></pre>
  
  <p>But I don't like it at all, any takers?</p>
  
  <p>I don't really want to dig out <code>RegEx</code> for this, do I?</p>
  
  <p>I know my string is going to have the term I'm searching for, so you can assume that...</p>
  
  <p>Of course for strings <em>where</em> <strong>length > 1</strong>,</p>
  
  <pre><code>string haystack = "/once/upon/a/time";
  string needle = "/";
  int needleCount = ( haystack.Length - haystack.Replace(needle,"").Length ) / needle.Length;
  </code></pre>