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.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources