title: String Indexes in swift
body: <pre><code>let greeting = "Hello World"
  greeting[greeting.startIndex]
  </code></pre>
  
  <p>This code will print first character of string "H"</p>
  
  <pre><code>let = "Hello World"
  greeting.startIndex
  greeting.endIndex
  </code></pre>
  
  <p>whats the difference? this thing don't do anything. Even i dont get error for</p>
  
  <pre><code>greeting.endIndex
  </code></pre>
  
  <p>of course I am not retrieving string value through subscript syntax but in the Substring topic I found</p>
  
  <pre><code>greeting.endIndex
  </code></pre>
  
  <p>it is print only</p>
  
  <pre><code>string.index
  </code></pre>
  
  <p>Ok let me explain why I am asking about <code>string.index</code> and <code>greeting.endIndex</code></p>
  
  <pre><code>let greeting = "Hello_world!"
  let index = greeting.index(of: "_") ?? greeting.endIndex
  let beginning = greeting[..&lt;index]
  // beginning is "Hello"
  </code></pre>
  
  <p>This code is related to substring, I totally understand what is it doing and in second line of code there is Nil-Coalescing Operator and you know what is it for. But what if</p>
  
  <pre><code>let greeting = "Hello world!"
  let index = greeting.index(of: "_") ?? greeting.endIndex
  let beginning = greeting[..&lt;index]
  // beginning is "Hello world"
  </code></pre>
  
  <p>If there is no <code>"_"</code> in string value means <code>greeting.index(of: "_")</code> is <code>nil</code> then it should be returns default value <code>greeting.endIndex</code> as Nil-Coalescing Operator does right. So why does <code>greeting.endIndex</code> returning `"Hello world!"</p>