如何在swift上使用数组来获取字符串的最后一个字符?
发布时间:2020-12-14 05:53:35 所属栏目:百科 来源:网络整理
导读:我有一个字符串 let stringPlusString = TextBoxCal.text 我想要得到stringPlusString的最后一个字符我不想在java上使用数组存在charAt,但是我无法在swift上找到这样的关键字 对于Swift 2.x和Swift 3: let str = "hello world?"let lastChar = str.charact
我有一个字符串
let stringPlusString = TextBoxCal.text 我想要得到stringPlusString的最后一个字符我不想在java上使用数组存在charAt,但是我无法在swift上找到这样的关键字
对于Swift 2.x和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 "?" 这适用于Swift 1.2和Swift 2.x: let str = "hello world?" let lastChar = str[str.endIndex.predecessor()] // lastChar is "?" 对于Swift 3: let str = "hello world?" let lastChar = str[str.index(before: str.endIndex)] // lastChar is "?" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |