Swift - 字符串(String)用法详解
(本文代码已升级至Swift4)
1,判断是否为空:isEmpty
var str:String = "" if str.isEmpty{ print("is empty") }
2,获取字符数量:string.count
let str = "hangge.com" print("\(str.count)个字符")
3,检查字符串是否有特定前缀/后缀:hasPrefix/hasSuffix
var str = "hangge.com" if str.hasSuffix(".com"){ }
4,还可以用“\()”在字符串里包裹变量,常量
let name = "hangge.com" let msg = "欢迎来到 \(name)"
5,多行字符串字面量(多行文字)
过去如果有一个很长的字符串也只能写在一行。到了 Swift 4 可以把字符串写在一对 """ 中,这样字符串就可以写成多行。let str = """ 欢迎访问hanggge.com 做最好的开发者知识平台 """ print(str)
6,大小写转换
通过字符串的 uppercased()、lowercase() 方法、capitalized 属性来访问一个字符串的大写/小写/首字母大写
let str = "Welecome to hangge.com" let uppercase = str.uppercased() //WELECOME TO HANGGE.COM let lowercase = str.lowercased() //welecome to hangge.com let capitalized = str.capitalized //Welecome To Hangge.Com
7,字符串截取
(1)将 String 转化为 NSString 再截取
let str = "Welecome to hangge.com" let fromStr = (str as NSString).substring(from: 5) //ome to hangge.com let toStr = (str as NSString).substring(to: 5) //Welec let rangeStr = (str as NSString).substring(with: NSMakeRange(4,1)) //c(2)直接调用 String 的对应方法(不推荐,substring 现已被废除)
let str = "Welecome to hangge.com" let index = str.index(str.startIndex, offsetBy: 5) let index2 = str.index(str.endIndex, offsetBy: -5) let range = Range(uncheckedBounds: (lower: index, upper: index2)) let fromStr = str.substring(from: index) //ome to hangge.com let toStr = str.substring(to: index) //Welec let rangeStr = str.substring(with: range) //ome to hangg现在还是推荐使用如下方式截取:
let str = "Welecome to hangge.com" let index = str.index(str.startIndex, offsetBy: 5) let index2 = str.index(str.endIndex, offsetBy: -5) let rangeStr = str[index..<index2] //ome to hangg
(3)Swift4 新增了一个语法糖 ... 可以对字符串进行单侧边界取子串。
let str = "Welecome to hangge.com" let index = str.index(str.startIndex, offsetBy: 5) let rangeStr0 = str[..<index] //Welec let rangeStr1 = str[...index] //Weleco let rangeStr2 = str[index...] //ome to hangge.com
(4)还可以通过 prefix 和 suffix 方法进行单边截取。
let str = "Welecome to hangge.com" let index = str.index(str.startIndex, offsetBy: 5) let rangeStr0 = str.prefix(5) //Welec let rangeStr1 = str.prefix(upTo: index) //Welec let rangeStr2 = str.prefix(through: index) //Weleco let rangeStr3 = str.suffix(5) //e.com let rangeStr4 = str.suffix(from: index) //ome to hangge.com
8,获取某个下标索引的前一个、后一个下标对应的字符
import UIKit let str = "hangge.com" //获取字符串开始索引后一个下标对应的字符 let startIndex = str.startIndex let char1 = str[str.index(after: startIndex)] //获取字符串结束索引前一个下标对应的字符 let endIndex = str.endIndex //endIndex在值上与字符串的长度相等 let char2 = str[str.index(before: endIndex)]
9,获取某个子串在父串中的范围
var range = str.range(of: "Hello")
10,获取某个子串在父串中的开始位置索引
参考我的另一篇文章:Swift - 在字符串中查找另一字符串首次出现的位置11,追加字符串(拼接字符串)
str.append("hangge.com")
12,在指定位置插入字符
(1)插入一个字符str.insert("a", at: str.index(str.startIndex, offsetBy: 5))(2)插入一组字符
str.insert(contentsOf: ["~","~","~"], at: str.index(str.startIndex, offsetBy: 5))
13,在指定范围替换一个字符串
str.replaceSubrange(str.startIndex...str.index(str.startIndex, offsetBy: 4), with: "hangge")
14,删除指定范围的字符串
str.removeSubrange(str.startIndex...str.index(str.startIndex, offsetBy: 2))
15,删除第一个字符
str.dropFirst()
16,字符串反转
let str1 = "hangge.com" let str2 = String(str1.reversed()) //moc.eggnah
17,遍历字符串里的字符
let str = "hangge.com" //方法1 for c in str { print(c) } //方法2 str.forEach { print($0) }
18,获取字符对应的ASCII编码
let character: Character = "a" let unicodeScalars = character.unicodeScalars let startIndex = unicodeScalars.startIndex let asciiCode = unicodeScalars[startIndex].value print(asciiCode)
19,map方法
let str = "hangge" _ = str.map { print($0.description) }
20,filter方法
let str = "hangge" let filtered = str.filter { $0 == "g" } print(filtered) //gg
21,reduce方法
let str = "hangge" let result = str.reduce("1") { (result, c) -> String in print(result, c) return result + String(c) } print("最终结果:\(result)")
substring也被干掉了,用这种
let newStr = str[..<index]
let newStr = str[index...]
航哥,advance在swift2.0 移除了 现在换成了:advancedBy
let index = str!.startIndex.advancedBy(5)