1,判断是否为空:isEmpty
var
str:
String
=
""
if
str.isEmpty{
print(
"is empty"
)
}
|
2,获取字符数量: string.characters.count
let str =
"hangge.com"
"\(str.characters.count)个字符"
)
|
3,检查字符串是否有特定前缀/后缀: hasPrefix/hasSuffix
5,大小写转换: 通过字符串的uppercaseString、lowercaseString、capitalizedString属性来访问一个字符串的大写/小写/首字母大写
1
2
3
4
5
|
let str =
"Welecome to hangge.com"
let uppercase = str.uppercaseString
//WELECOME TO HANGGE.COM
let lowercase = str.lowercaseString
//welecome to hangge.com
let capitalized = str.capitalizedString
//Welecome To Hangge.Com
|
6,字符串截取 (1)将String转化为NSString再截取