title: Remove trailing and leading spaces and extra internal whitespace with one gsub call
body: <p>I know you can remove trailing and leading spaces with </p>
  
  <pre><code>gsub("^\\s+|\\s+$", "", x)
  </code></pre>
  
  <p>And you can remove internal spaces with</p>
  
  <pre><code>gsub("\\s+"," ",x)
  </code></pre>
  
  <p>I can combine these into one function, but I was wondering if there was a way to do it with just one use of the <code>gsub</code> function</p>
  
  <pre><code>trim &lt;- function (x) {
    x &lt;- gsub("^\\s+|\\s+$|", "", x)
    gsub("\\s+", " ", x)
  }
  
  testString&lt;- "  This is a      test. "
  
  trim(testString)
  </code></pre>