美文网首页
Swift——字符串的截取

Swift——字符串的截取

作者: 闲云悠鹤蝶恋舞 | 来源:发表于2018-02-28 11:52 被阅读13次
1、使用suffix移除前面的字符串
let str = ",hello!"
// 移除最前面的逗号
let index = str .index(str .startIndex, offsetBy: 1)
str = String(str .suffix(from: index))
2、使用prefix通过指定位置移除后面的几位
// 通过指定位置移除后面的几位
 let currentDateStr = "2018-02-24 10:03:58"
 let index = currentDateStr.index(currentDateStr.startIndex, offsetBy: 10)
 let result = currentDateStr.prefix(upTo: index)
 打印结果如下:"2018-02-24”
3、使用prefix通过指定字符来移除字符后面的几位
// 通过指定字符来移除字符后面的几位
str = "150公斤/天"
let index = str?.index(of: "公")
let subStr = String(describing: str?.prefix(upTo: index!))
结果:150

相关文章

网友评论

      本文标题:Swift——字符串的截取

      本文链接:https://www.haomeiwen.com/subject/igebxftx.html