title: Swift - regex vs components/join: which is more efficient for String operations
body: <p>I need to remove excessive spaces between words in Swift (there won't be more than 5 words in a given string), not sure which is more efficient: regular expression or normal components and join?</p>
  
  <p>For example:</p>
  
  <pre><code>Hello   world! My name is     Tom.
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Hello world! My name is Tom.
  </code></pre>
  
  <p>Method 1:</p>
  
  <pre><code>let result = input.components(separatedBy: " ").filter { string -&gt; Bool in
      return string.isEmpty == false
  }.joined(separator: " ")
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>let result = input.replacingOccurrences(of: "[ \t]+", with: " ", options: .regularExpression, range: range)
  </code></pre>