How does String substring work in Swift
<p>I've been updating some of my old code and answers with Swift 3 but when I got to Swift Strings and Indexing with substrings things got confusing. </p>

<p>Specifically I was trying the following:</p>

<pre><code>let str = "Hello, playground"
let prefixRange = str.startIndex..&lt;str.startIndex.advancedBy(5)
let prefix = str.substringWithRange(prefixRange)
</code></pre>

<p>where the second line was giving me the following error</p>

<blockquote>
  <p>Value of type 'String' has no member 'substringWithRange'</p>
</blockquote>

<p>I see that <code>String</code> does have the following methods now:</p>

<pre><code>str.substring(to: String.Index)
str.substring(from: String.Index)
str.substring(with: Range&lt;String.Index&gt;)
</code></pre>

<p>These were really confusing me at first so I started playing around <a href="https://stackoverflow.com/questions/39676939/how-does-string-index-work-in-swift-3">index and range</a>. This is a followup question and answer for substring. I am adding an answer below to show how they are used.</p>