1. 程式人生 > >swift-字串的擷取

swift-字串的擷取

// 1, 字串的擷取
let url = "www:xiaoyu.com"
// 1.1 方式一:將string型別轉成nsstring   as NSString
let header = (url as NSString).substring(to: 3)
let range = NSMakeRange(4, 8)
let middle = (url as NSString).substring(with: range)

// 1.2 方式二:直接使用string
let headerIndex = url.index(url.startIndex, offsetBy: 3)
let header1 = url.substring(to
: headerIndex) let startIndex = url.index(url.startIndex, offsetBy: 4) let endIndex = url.index(url.startIndex, offsetBy: 10) let range2 = Range(startIndex..<endIndex) let middle2 = url.substring(with: range2) let strart3 = url.index(url.startIndex, offsetBy: 11) let end3 = url.index(url.startIndex, offsetBy: 14
) let rang3 = Range(strart3..<end3) let endStr = url.substring(with: rang3)