Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
communitycreator

Using the first property of string to get last character in Swift

Theodore Kelechukwu Onyejiaku

Overview

In Swift, we can use the last property of every string to get the last character of any character.

Syntax

string.last!

Return value

This property returns a value that is the last character of a string.

Example

Let’s look at the code below:

// create strings
let name = "Theodore"
let greetings = "Welcome to Edpresso!"

// print last characters of strings.
print(name.last!)
print(greetings.last!)
Getting last characters of a string in Swift

Explanation

  • Lines 2 and 3: We create two string values.
  • Lines 6 and 7: We use the last property of the string class and get the last characters of the strings we created. And finally, we print the values to the console.

RELATED TAGS

swift
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring