let str = “Hello, Swift!”
let prefix = str.prefix(5)
print(prefix) // “Hello“
截取后 n 个字符:
let str = “Hello, Swift!”
let suffix = str.suffix(6)
print(suffix) // “Swift!”
截取指定范围内的字符:
let str = “Hello, Swift!”
let range = str.index(str.startIndex, offsetBy: 7)..<str.endIndex
let subString = str[range]
print(subString) // “Swift!”
截取从某个位置到末尾的字符:
let str = “Hello, Swift!”
let index = str.index(str.startIndex, offsetBy: 7)
let subString = str[index…]
print(subString) // “Swift!”
从最后一个指定“.”字符开始截取,到字符串结束:
let str = “http://www.free.com/346345647457.png“
if let dotIndex = str.lastIndex(of: “.”) {
let fileExtension = str[dotIndex+1..<str.endIndex]
print(fileExtension) // “png“
原文地址:https://blog.csdn.net/u011463507/article/details/132165877
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_29484.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!