How to get the first letter of a string in Swift

Overview

In Swift, the first property is used to return the first character of a string.

Syntax

string.first

Return Value

This property returns the first character of a string.

Example

// create strings
let name = "Theodore"
let greetings = "Welcome to Edpresso!"
// print first characters of strings.
print(name.first!)
print(greetings.first!)

Explanation

  • Line 2 and 3: We create two string values.
  • Line 6 and 7: We used the first property to get the first characters of the strings. Next, we print the values to the console.

Free Resources