慕标琳琳
在Swift 4中:获取字符串中的字符索引:let str = "abcdefghabcd"if let index = str.index(of: "b") { print(index) // Index(_compoundOffset: 4, _cache: Swift.String.Index._Cache.character(1))}使用Swift 4从String创建SubString(前缀和后缀):let str : String = "ilike"for i in 0...str.count { let index = str.index(str.startIndex, offsetBy: i) // String.Index let prefix = str[..<index] // String.SubSequence let suffix = str[index...] // String.SubSequence print("prefix \(prefix), suffix : \(suffix)")}产量prefix , suffix : ilikeprefix i, suffix : likeprefix il, suffix : ikeprefix ili, suffix : keprefix ilik, suffix : eprefix ilike, suffix : 如果要生成2个索引之间的子字符串,请使用:let substring1 = string[startIndex...endIndex] // including endIndexlet subString2 = string[startIndex..<endIndex] // excluding endIndex