-
Swift
的字符串类型String
,跟OC
的NSString
,在API
设计上有很大差别.
//空字符串
var emptyStrin1 = ""
var emptyStrin2 = String()
var str = "123456"
print(str.hasPrefix("123"))//true
print(str.hasSuffix("456"))//true
var string: String = "1"
//拼接
string.append("_2")//1_2
//重载运算符 +
string = string + "_3"//1_2_3
//重载运算符 +=
string += "_4"//1_2_3_4
//\()字符串插值
string = "\(string)_5"//1_2_3_4_5
//长度:9,1_2_3_4_5
print(string.count)
String的插入和删除
var str = "1_2"
str.insert("_", at: str.endIndex)//1_2_
str.insert(contentsOf: "3_4", at: str.endIndex)//1_2_3_4
str.insert(contentsOf: "666", at: str.index(after: str.startIndex))//1666_2_3_4
str.insert(contentsOf: "888", at: str.index(before: str.endIndex))//1666_2_3_8884
str.insert(contentsOf: "hello", at: str.index(str.startIndex, offsetBy: 4))//1666hello_2_3_8884
//@inlinable public func firstIndex(of element: Self.Element) -> Self.Index?
//返回值为可选类型,字符串中没有"1",那么返回值为nil
//这里确定肯定含有"1",所以强制解包
//移除第一个"1"元素,如果字符串有多个"1",只移除第一个查找到的"1"
str.remove(at: str.firstIndex(of: "1")!)
print(str)//666hello_2_3_8884
str.removeAll { (indexString) -> Bool in
//如果该位置的字符为"6",那么返回true
//该方法的作用移除str中所有"6"
indexString == "6"
}
print(str)//hello_2_3_8884
//等价写法
str.removeAll {//字符串已经不包含"6"
$0 == "6"
}
print(str)//hello_2_3_8884
//hello_2_3_8884 长度为14 range = (14 - 4) ..< 14
//10 ..< 14 ,从前面书第10个字符为_ 11: 8,12:8,13:8
//所以移除的字符串为"_888"
let range = str.index(str.endIndex, offsetBy: -4) ..< str.index(before: str.endIndex)
str.removeSubrange(range)
print(str)//hello_2_3_4
SubString
-
string
可以通过下标,prefix
suffix
等街区子串,子串类型不是String
,而是Substring
-
Substring
和它的base
共享字符串数据 -
substring
转为String
时,会重新分配新的内存存储字符串数据
let str = "1_2_3_4_5"
let sub1 = str.prefix(3)
print(sub1)//1_2
let sub2 = str.suffix(3)
print(sub2)//4_5
let range = str.startIndex ..< str.index(str.startIndex, offsetBy: 3)
print(range)//Index(_rawBits: 1)..<Index(_rawBits: 196865)
let sub3 = str[range]
print(sub3)//1_2
print(sub3.base)//1_2_3_4_5
let str2 = String(sub3)
print(str2)//1_2
String与Character
for c in "jack" {//c 是Character类型
print(c)
print(type(of: c))//Character
}
let str = "jack"
let c = str[str.startIndex]
print(c)//j
print(type(of: c))//Character
String的相关协议
-
BidirectionalCollection
协议包含的部分内容
1.startIndex
endIndex
属性,index
方法
2.String
Array
都遵守了这个协议 -
RangeReplaceableCollection
协议包含的部分内容
1.append
insert
remove
方法
2.String
Array
都遵守了这个协议 -
Dictionary
Set
也有实现上述协议中声明的一些方法,只是并没有遵守上述协议.
多行String
- 单引号修饰的多行字符串会报错,提示要使用
"""
let str = "
1
"2"
3
'4'
"
image.png
override func viewDidLoad() {
let str = """
1
"2"
3
'4'
"""
print(str)
}
//打印结果:
1
"2"
3
'4'
- 如果要显示3引号,至少转义1个引号
override func viewDidLoad() {
let str = """
1 \"""
"2" \"\""
3 \"\"\"
'4'
"""
print(str)
}
//打印结果:
1 """
"2" """
3 """
'4'
-
缩进以结尾的3引号为对齐线
如下写法会报错.以结尾的3引号为对齐线之前的都报错.
image.png
修复后
class HomeViewController: UIViewController {
override func viewDidLoad() {
let str = """
1
2
3
4
"""
print(str)
}
}
//打印结果
1
2
3
4
- 以下字符串是等价的
let str1 = "111111111111"
let str = """
111111111111
"""
print(str == str1)//true
String与NSString
-
String
与NSString
之间可以随时的桥接转换 - 比较字符串内容是否等价
1.String
使用==
运算符
2.NSString
使用isEqual
方法,也可以使用==
运算符.本质还是调用了isEqual
方法
let str1: String = "jack"
let str2: NSString = "rose"
print(str1)
print(str2)
let str3 = str1 as NSString
let str4 = str2 as String
print(str3)
print(str4)
//ja
let str5 = str3.substring(with: NSRange(location: 0, length: 2))
print(str5)
let str1: String = "jack"
let str2: NSString = "jack"
print(str2 as String == str1)//true
print(str1 == str2 as String)//true
print(str2.isEqual(to: str1))//true
print(str2.isEqual(str1))//true
print(str1.isEqual(str2))//true
Swift OC桥接转换表
String | <--> | NSString |
String | <-- | NSMutableString |
Array | <--> | NSArray |
Array | <-- | NSMutableArray |
Dictionary | <--> | NSDictionary |
Dictionary | <-- | NSMutableDictionary |
Set | <--> | NSSet |
Set | <-- | NSMutableSet |
网友评论