What is the startIndex property of a string in Swift?
Overview
The startIndex property of a string in Swift returns the position of the first character of a given string. This property only works if the string is not empty.
Syntax
str.startIndex
Code example
// create stringslet str1 = "Edpresso"let str2 = "is"let str3 = "Awesome"// use the startIndex propertyprint(str1.startIndex)print(str2.startIndex)print(str3.startIndex)
Code explanation
- Lines 2–4: We create the strings
str1,str2, andstr3. - Lines 7–9: We use the
startIndexproperty on the strings we created earlier. Then, we print the results to the console.