title: Count characters in a string (excluding spaces) in R?
body: <p>I want to count the number of characters in a string (excluding spaces) and I'd like to know if my approach can be improved.</p>
  
  <p>Suppose I have:</p>
  
  <pre><code>x &lt;- "hello to you"
  </code></pre>
  
  <p>I know <code>nchar()</code> will give me the number of characters in a string (including spaces):</p>
  
  <pre><code>&gt; nchar(x)
  [1] 12
  </code></pre>
  
  <p>But I'd like to return the following (excluding spaces):</p>
  
  <pre><code>[1] 10
  </code></pre>
  
  <p>To this end, I've done the following:</p>
  
  <pre><code>&gt; nchar(gsub(" ", "",x))
  [1] 10
  </code></pre>
  
  <p>My worry is the <code>gsub()</code> will take a long time over many strings. Is this the correct way to approach this, or is there a type of nchar'esque function that will return the number of characters without counting spaces?</p>
  
  <p>Thanks in advance.</p>