我有一个字符串
let stringPlusString = TextBoxCal.text
我想获取 stringPlusString
的最后一个字符。我不想使用数组。
Java 有 charAt
但我在 Swift 中找不到类似的东西
最佳答案
使用last
获取最后一个Character
对于 Swift 4:
let str = "hello world😍"
let lastChar = str.last! // lastChar is "😍"
对于 Swift 2 和 Swift 3:
let str = "hello world😍"
let lastChar = str.characters.last! // lastChar is "😍"
对于 Swift 1.2:
let str = "hello world😍"
let lastChar = last(str)! // lastChar is "😍"
下标 String
以获取最后一个 Character
这适用于 Swift 3 和 Swift 4:
let str = "hello world😍"
let lastChar = str[str.index(before: str.endIndex)] // lastChar is "😍"
对于 Swift 1.2 和 Swift 2:
let str = "hello world😍"
let lastChar = str[str.endIndex.predecessor()] // lastChar is "😍"
关于swift - 不使用数组获取字符串的最后一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25113672/