swift - 不使用数组获取字符串的最后一个字符?

标签 swift string

我有一个字符串

let stringPlusString = TextBoxCal.text

我想获取 stringPlusString 的最后一个字符。我不想使用数组。

Java 有 charAt 但我在 Swift 中找不到类似的东西

最佳答案

使用last获取最后一个Character

对于 Swift 4:

let str = "hello world😍"
let lastChar = str.last!   // lastChar is "😍"

对于 Swift 2Swift 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 3Swift 4:

let str = "hello world😍"
let lastChar = str[str.index(before: str.endIndex)]   // lastChar is "😍"

对于 Swift 1.2Swift 2:

let str = "hello world😍"
let lastChar = str[str.endIndex.predecessor()]   // lastChar is "😍"

关于swift - 不使用数组获取字符串的最后一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25113672/

相关文章:

ios - 重新加载 UINavigationItem

ios - 如何在 iOS 中从相机流式传输实时视频

php - 如何从特定字符后开始删除字符串

python - 如何在 Python 中迭代一个 Unicode 字符串?

c - C 中字符串前的 "*"是什么意思?

swift - 具有核心数据问题的搜索栏

ios - 如果我执行 unwind segue (show),母 View Controller 如何获取 subview Controller ?

python - 如何将文本文件读入字符串变量并去除换行符?

swift - 在 Swift 中,我应该编写一个单独的类来处理 UI 组件吗?

python Pandas : Find a value in another dataframe and replace it