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 strings
let str1 = "Edpresso"
let str2 = "is"
let str3 = "Awesome"
// use the startIndex property
print(str1.startIndex)
print(str2.startIndex)
print(str3.startIndex)

Code explanation

  • Lines 2–4: We create the strings str1, str2, and str3.
  • Lines 7–9: We use the startIndex property on the strings we created earlier. Then, we print the results to the console.

Free Resources