title: How to replace every second space, but not first/last with "&nbsp;" in a given string?
body: <p>let's say I have the following string:</p>
  
  <pre><code>string s = "A B  C   D    TESTTEST  BLA BLA      TEST       TEST2"
  </code></pre>
  
  <p>Then I need the following:</p>
  
  <pre><code>"A B &amp;nbsp;C &amp;nbsp; D &amp;nbsp;&amp;nbsp; TESTTEST &amp;nbsp;BLA BLA &amp;nbsp; &amp;nbsp;&amp;nbsp; TEST &amp;nbsp; &amp;nbsp; &amp;nbsp; TEST2"
  </code></pre>
  
  <p>So the rules are:<br>
  a) replace every second space (<strong>between non space chars</strong>) with <code>&amp;nbsp;</code><br>
  b) If the last space is replaced with <code>&amp;nbsp;</code>, try to move it backwards (if possible) one step so the next word is not touched by the force space.  </p>
  
  <p>Background:</p>
  
  <p>I want to use that to print data from the database to my website. But I want to optimize the forced spaces to use less space. I also want the last forced space not to touch the next word (if possible) so it is easier for some search engines to catch that word.</p>
  
  <p>Will I need to loop through every char in that string and count the occurrence or is there an easier, faster and more fancy way?</p>
  
  <p><strong>Thank you, both solutions are working perfect.</strong><br>
  So I did a benchmark to figure out which solution to accept:</p>
  
  <p>@Guffa, your solution needs 22 seconds for 1 Million runs<br>
  @Timwi, your solution needs 7 seconds for 1 Million runs</p>
  
  <p>I will give you both an upvote, but I will accept Timwi's solution.</p>