title: Trim whitespace from a String
body: <p>I know there are several ways to do this in Java and C that are nice, but in C++ I can't seem to find a way to easily implement a string trimming function.</p>
  
  <p>This is what I currently have:</p>
  
  <pre><code>string trim(string&amp; str)
  {
      size_t first = str.find_first_not_of(' ');
      size_t last = str.find_last_not_of(' ');
      return str.substr(first, (last-first+1));
  }
  </code></pre>
  
  <p>but whenever I try and call </p>
  
  <pre><code>trim(myString);
  </code></pre>
  
  <p>I get the compiler error </p>
  
  <pre><code>/tmp/ccZZKSEq.o: In function `song::Read(std::basic_ifstream&lt;char, 
  std::char_traits&lt;char&gt; &gt;&amp;, std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*, char const*)':
  song.cpp:(.text+0x31c): undefined reference to `song::trim(std::string&amp;)'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I am trying to find a simple and standard way of trimming leading and trailing whitespace from a string without it taking up 100 lines of code, and I tried using regex, but could not get that to work as well.</p>
  
  <p><strong>I also cannot use Boost.</strong></p>