Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
character

How to get the first letter of a string in Swift

Theodore Kelechukwu Onyejiaku

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!)
Get the first character of a string in Swift

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.

RELATED TAGS

swift
character

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring