How to get the last character from a string in Swift

3 Answers

0 votes
let s = "swift"
let lastCharacter = s.last!

print(lastCharacter)



/*
run:

t

*/

 



answered Feb 24, 2021 by avibootz
0 votes
let s = "swift"
let lastCharacter = s[s.index(before: s.endIndex)] 

print(lastCharacter)



/*
run:

t

*/

 



answered Feb 24, 2021 by avibootz
0 votes
let s = "swift"
let lastCharacter = s.suffix(1)

print(lastCharacter)



/*
run:

t

*/

 



answered Feb 24, 2021 by avibootz
...