title: Removing leading and trailing spaces from a string
body: <p>How to remove spaces from a string object in C++.<br>
  For example, how to remove leading and trailing spaces from the below string object.  </p>
  
  <pre><code>//Original string: "         This is a sample string                    "
  //Desired string: "This is a sample string"
  </code></pre>
  
  <p>The string class, as far as I know, doesn't provide any methods to remove leading and trailing spaces.  </p>
  
  <p>To add to the problem, how to extend this formatting to process extra spaces between words of the string. For example,</p>
  
  <pre><code>// Original string: "          This       is         a sample   string    " 
  // Desired string:  "This is a sample string"  
  </code></pre>
  
  <p>Using the string methods mentioned in the solution, I can think of doing these operations in two steps.</p>
  
  <ol>
  <li>Remove leading and trailing spaces.</li>
  <li>Use <strong>find_first_of, find_last_of, find_first_not_of, find_last_not_of and substr</strong>, repeatedly at word boundaries to get desired formatting.</li>
  </ol>