let greeting = "Hello World"
greeting[greeting.startIndex]
This code will print first character of string "H"
let = "Hello World"
greeting.startIndex
greeting.endIndex
whats the difference? this thing don't do anything. Even i dont get error for
greeting.endIndex
of course I am not retrieving string value through subscript syntax but in the Substring topic I found
greeting.endIndex
it is print only
string.index
Ok let me explain why I am asking about string.index
and greeting.endIndex
let greeting = "Hello_world!"
let index = greeting.index(of: "_") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning is "Hello"
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
let greeting = "Hello world!"
let index = greeting.index(of: "_") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning is "Hello world"
If there is no "_"
in string value means greeting.index(of: "_")
is nil
then it should be returns default value greeting.endIndex
as Nil-Coalescing Operator does right. So why does greeting.endIndex
returning `"Hello world!"