title: Least convoluted way of extracting parts of string in Swift 4
body: <p>If I want to extract parts of a string in Swift 4 (when building a Mac app in Xcode 10), are there less convoluted ways of extracting said parts? The code below works, but to me it's nearly unreadable.</p>
  
  <p>
  
  <pre><code>let myString = "foobar"
  let myShorterString = String(myString[myString.index(myString.startIndex, offsetBy: 2)..&lt;myString.index(myString.startIndex, offsetBy: 5)])
  </code></pre>
  
  <p>I want to end up with <code>oba</code> in this case.</p>
  
  <p>Some documentation claims that <code>"foobar"[2...5]</code> should work, but to me that throws the error <code>cannot subscript String with an integer range</code>. Also, 'substring(to:)' has reportedly been deprecated.</p>
  
  <p>I am aware of <a href="https://stackoverflow.com/a/38215613/2450941">Leo Dabus' thorough answer</a>, but even though that seems prudent, it has approx. 60 lines of code, so I think of that as more convoluted. I'm looking for <em>less</em> convoluted answers, and if there are none, confirmation that this is the simplest solution.</p>
  
  <p><strong>Answer:</strong></p>
  
  <pre><code>let myShorterString = String(Array(myString)[2...4])
  </code></pre>
  
  <p>Courtesy of <a href="https://stackoverflow.com/a/47229985/2450941">Harsh G.'s answer</a>.</p>