Get the last character of a string without using array?

Using last to get the last Character

For Swift 4:

let str = "hello world๐Ÿ˜"
let lastChar = str.last!   // lastChar is "๐Ÿ˜"

For Swift 2 and Swift 3:

let str = "hello world๐Ÿ˜"
let lastChar = str.characters.last!   // lastChar is "๐Ÿ˜"

For Swift 1.2:

let str = "hello world๐Ÿ˜"
let lastChar = last(str)!   // lastChar is "๐Ÿ˜"

Subscripting the String to get the last Character

This works for Swift 3 and Swift 4:

let str = "hello world๐Ÿ˜"
let lastChar = str[str.index(before: str.endIndex)]   // lastChar is "๐Ÿ˜"

And for Swift 1.2 and Swift 2:

let str = "hello world๐Ÿ˜"
let lastChar = str[str.endIndex.predecessor()]   // lastChar is "๐Ÿ˜"

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)