title: How do you use String.substringWithRange? (or, how do Ranges work in Swift?)
body: <p>I have not yet been able to figure out how to get a substring of a <code>String</code> in Swift:</p>
  
  <pre><code>var str = “Hello, playground”
  func test(str: String) -&gt; String {
   return str.substringWithRange( /* What goes here? */ )
  }
  test (str)
  </code></pre>
  
  <p>I'm not able to create a Range in Swift. Autocomplete in the Playground isn’t super helpful - this is what it suggests:</p>
  
  <pre><code>return str.substringWithRange(aRange: Range&lt;String.Index&gt;)
  </code></pre>
  
  <p>I haven't found anything in the Swift Standard Reference Library that helps. Here was another wild guess:</p>
  
  <pre><code>return str.substringWithRange(Range(0, 1))
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>let r:Range&lt;String.Index&gt; = Range&lt;String.Index&gt;(start: 0, end: 2)
  return str.substringWithRange(r)
  </code></pre>
  
  <p>I've seen other answers (<a href="https://stackoverflow.com/questions/24029163/finding-index-of-character-in-swift-string">Finding index of character in Swift String</a>) that seem to suggest that since <code>String</code> is a bridge type for <code>NSString</code>, the "old" methods should work, but it's not clear how - e.g., this doesn't work either (doesn't appear to be valid syntax):</p>
  
  <pre><code>let x = str.substringWithRange(NSMakeRange(0, 3))
  </code></pre>
  
  <p>Thoughts?</p>