字符串
//: 字符串字面量. 包含双引号""的值成为字符串字面量.
"回到洪荒,去支配,去操纵"
//: - 空字符串: 用""字面量
var a = ""
var b = " "
//: - 判断字符串是否为空
a.isEmpty
b.isEmpty
*使用三引号创建多段字符串,必须换行,中间的引号不需要转移(Swift 4.0)
let a = """
sajopdjao"
asodja
aass
"""
字符
- 用Character类型来定义字符
var a: Character = "a"
var b: Character = "b"
- 可以对一个字符串的characters属性进行循环,来访问其中单个字符.
let words = "Swift4"
for word in words.characters {
print(word)
}
字符串基本操作
- 连接字符串 用 +
let a = "腾讯"
let b = "微信"
let c = "王者荣耀"
var famous = a + b + c
向字符串添加字符, 用append方法
let number: Character = "1️⃣"
famous.append(number)
- 字符串插值:组合常量/变量/字面量/表达式成一个长字符串
//: 字符串插值:组合常量/变量/字面量/表达式成一个长字符串
let name = "李白"
let type = "G"
let number = 11
let price = 158.5
let 订票提示 = "\(name)先生, 您订购了\(type)\(number)的往返票,需支付\(price * 2)元."
- 特殊字符:
\0 \ \t \n \r " ' \u{n}
- 字符计数: 使用字符串characters属性的count属性来获取字符个数.Swift4.0后String又改成集合类型,所以也可以用string.count
let c = "abc!!!"
//2.1 获取字节长度
let a = "你好"
print("a 的字节长度为\(a.lengthOfBytes(using: String.Encoding.unicode))")
//获取字符长度,大多数情况下这个
c.characters.count Swift2.x ~ Swift3.x
c.count //Swift4
- 字符串裁剪 , components方法
let string = "a&b&c&d"
print(string.components(separatedBy: "&"))
*字符串遍历
var greeting = "hello world"
//通过字符集
for char in greeting.characters {
print(char)
}
//通过索引
for index in greeting.indices {
print(greeting[index])
}
*Swift4 引入新内容substring, 官方代码
let greeting = "Hello, world!"
let index = greeting.index(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning is "Hello"
let range = greeting.index(greeting.startIndex, offsetBy: 3) ..< greeting.endIndex
print(greeting[range])
但是注意SubString可能会用到原来string的内存。官方建议是短时间使用,如果想要长时间保持,需要转为String
// Convert the result to a String for long-term storage.
let newString = String(beginning)
修改字符串-索引.
- 字符串索引: 索引对应其中每一个字符的位置.
let a = "www.baidu.com"
//首字符索引 startIndex, endIndex是尾字符后一个位置.
a.startIndex
a.endIndex
- 找到某个字符对应的索引
let greeting = "Hello World"
//Swift4
greeting.index(of: "W")
//Swift3
greeting.characters.index(of: "W")
- 确定索引后,用下标来访问相应字符
a[a.startIndex]
- 用字符串的字符数组的index(after:)或index(before)引用后一个或前一个索引
//Swift4
greeting.index(before: <#T##String.Index#>)
greeting.index(after: <#T##String.Index#>)
//Swift3
a[a.characters.index(after: a.startIndex)]
a[a.characters.index(before: a.endIndex)]
- 用字符串的字符数组的index(_:offsetBy:)方法向前进位
a[a.index(a.startIndex, offsetBy: 3)] //Swift4
a[a.characters.index(a.startIndex, offsetBy: 3)] //Swift3
- 字符串characters属性的indices属性表示索引的区间.
for b in a.characters.indices {
// print(a[b])
print(a[b], terminator: "-")
}
修改字符串-插入和删除
var a = "Swift"
//: - 插入一个字符
a.insert("!", at: a.endIndex)
//: - 插入一个字符串
let greeting = " hello world"
greeting.insert(contentsOf: "aaa", at: greeting.startIndex)
greeting.insert("a", at: greeting.startIndex)
//: - 删除一个指定索引的字符,用remove(at: index)方法.
greeting.remove(at: greeting.startIndex)
//: - 删除一个范围的子串,用removeRange方法. 删除之前添加的字符串
//Swift4
let startIndex = greeting.index(greeting.startIndex, offsetBy: 3)
let range = startIndex..<greeting.endIndex
greeting.removeSubrange(range)
比较字符串: Swift提供3种方法比较文本值: 字符串和字符相等性,前缀相等,后缀相等
- 字符串和字符相等性:用 == 或 != 操作符. Unicode扩展语素组合而成的字符与正式的单个字符被视为相等.
let x = "\u{1112}\u{1161}\u{11ab}"
"\u{1112}"
"\u{1161}"
"\u{11ab}"
let y = "\u{d55c}"
x == y
- 前缀和后缀相等: 用于检查一个字符串是否具有特定前缀和后缀. hasPrefix和hasSuffix
let a = "www.baidu.com"
a.hasPrefix("www")
b.hasSuffix("com")
重要,NSString和String的区别
- NSString是引用类型。SwiftString是值类型。
var nsString:NSString = "dsx"
var swiftString:String = "dsx"
NSString的意思是初始化了一个指针指向了这个字符串,但SwiftString的意思则是把字符串字面量赋值给变量。
- SwiftString字符串之间的拼接比NSString方便
NSString需要用append或者stringWithFormat将两个字符串拼接
NSString *strA = @"My name";
NSString *strB = @" is dsx";
strA = [strA stringByAppendingString:strB];
而SwiftString只需要用 “+” 即可
var strA = "My name"
var strB = " is dsx"
strA = "My name" + " is dsx"
strA = strA + strB
print(strA)
- SwiftString 可以实现字符串遍历, NSString 不可以
for character in "baidu.com".characters {
print(character)
}
- SwiftString计算字符串长度的方法与NSString不同
var strA:String = "mc梦"
var strB:NSString = "mc梦"
print(strA.characters.count)) // 3
print(strB.length); // 3
- SwiftString比较字符串相等的方式与NSString不同
NSString 有一个方法isEqualToString(Swift3.0简化API为isEqual) 方法用来判断两个字符串是否完全相等,String没有这个方法,但是因为String是值类型所以可以直接用 == 判断是否完全相等。
let nsStr1: NSString = "hello"
let nsStr2: NSString = "hello"
let swiftStr1: String = "hello"
let swiftStr2: String = "hello"
//NSString的方法, isEqualToString,Swift3.0简化
nsStr1.isEqual(to: nsStr2)
//String 没有isEqual方法
swiftStr1 == swiftStr2
- NSString可以同基本数据类型见转化
var strA:NSString = "12306"
var strB:NSString = "0.618"
var numOfInt = strA.integerValue;
var numOfDouble = strB.doubleValue;
- SwiftString可以通过isEmpty属性来判断该字符串是否为空
greeting.isEmpty
网友评论