Swift中常见的String用法,Array高阶使用,Set集合操作
Posted 滴水微澜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift中常见的String用法,Array高阶使用,Set集合操作相关的知识,希望对你有一定的参考价值。
String字符串常见用法
生成字符串
创建字符串 let greeting = "Hello, world!" let name = String("John") 连接字符串:使用加号(+)或者字符串插值(使用())来将多个字符串连接起来。 var firstName = "John" let lastName = "Doe" let fullName = firstName + " " + lastName // "John Doe" let fullName1 = firstName.append(lastName) // "My name is John Doe." let message = "My name is \\(fullName)." // "My name is John Doe."
子字符串查询,替换,插入,删除
查找子字符串 let sentence = "Swift is a powerful and intuitive programming language." if sentence.contains("Swift") // do something let range = sentence.range(of: "powerful") // Optional(Range(10..<18)) var str3 = "123456" print(str.hasPrefix("123")) print(str.hasSuffix("456")) 替换字符串 var sentence = "Swift is a powerful and intuitive programming language." sentence = sentence.replacingOccurrences(of: "powerful", with: "amazing") 字符串删除 // 666hello_2_3_8884 str4.remove(at: str4.firstIndex(of: "1")!) // hello_2_3_8884 str4.removeAll $0 == "6" var range = str4.index(str4.endIndex, offsetBy: -4)..<str4.index(before: str4.endIndex) // hello_2_3_4 str4.removeSubrange(range) 字符串插入 var str = "Hello, world!" let firstChar = str[str.startIndex] // "H" let lastChar = str[str.index(before: str.endIndex)] // "!" str.insert("!", at: str.endIndex) // "Hello, world!!"
字符串转数组
字符串分割 let names = "John, Jane, Jim" let arr = names.components(separatedBy: ", ") // ["John", "Jane", "Jim"]
字符串转其他
let str = "123" let num = Int(str) // Optional(123) let uppercased = str.uppercased() // "123"
Array数组高阶操作
map:对给定数组每个元素,执行闭包中的映射,将映射结果放置在数组中返回。
flatMap:对给定数组的每个元素,执行闭包中的映射,对映射结果进行合并操作,然后将合并操作后的结果放置在数组中返回。
compactMap:对给定数组的每个元素,执行闭包中的映射,将非空的映射结果放置在数组中返回。
compactMap:对给定数组的每个元素,执行闭包中的映射,将非空的映射结果-键值对放置在字典中返回。
filter:对给定数组的每个元素,执行闭包中的操作,将符合条件的元素放在数组中返回。
reduce:对给定数组的每个元素,执行闭包中的操作对元素进行合并,并将合并结果返回。
var arr = [1, 2, 3, 4] // [2, 4, 6, 8] var arr2 = arr.map $0 * 2 print(arr2) // [2, 4] var arr3 = arr.filter $0 % 2 == 0 print(arr3) // 10 var arr4 = arr.reduce(0) $0 + $1 print(arr4) // 10 var arr5 = arr.reduce(0, +) print(arr5)
Set集合操作
集合创建
let setA = Set(["a","b","c"]) let setB: Set = ["a","b","c"]
增删改查
setA.contains("a") setA.insert("c") setA.remove("a")
集合运算
let set1 = Set([1,2,3]) let set2 = Set([1,2]) //运算判断 if set1 == set2 // 子集,超集判断 set2.isSubset(of: set1) set1.isSuperset(of: set2) // 并集,交集 set1.union(set2) set1.intersection(set2)
Swift数组的加法运算符用法:array1 += array2
var stringList1 = [String]() //创建String类型空数组 var stringList2 = ["1", "3", "5", "7", "zoo", "9","zoo"] var stringList3 :[String] = ["2", "4", "6","apple", "8", "10"] stringList1 += stringList2 //把stringList2数组的所有元素添加到stringList1的末尾 print("stringList1: \(stringList1)") //输出stringList1 stringList1 += stringList3 //把stringList3数组的所有元素添加到stringList1的末尾 print("stringList1: \(stringList1)") //输出stringList1
运行结果:
stringList1: ["1", "3", "5", "7", "zoo", "9", "zoo"] stringList1: ["1", "3", "5", "7", "zoo", "9", "zoo", "2", "4", "6", "apple", "8", "10"]
以上输出结果的元素无序排列,可使用数组的 sort() 方法或 sortInPlace() 方法排序,两者不同在于
sort()方法有返回值,返回一个新数组,而不改变原数组的值:
print("After sort: \(stringList1.sort())") print("stringList1: \(stringList1)")
运行结果:
After sort: ["1", "10", "2", "3", "4", "5", "6", "7", "8", "9", "apple", "zoo", "zoo"] stringList1: ["1", "3", "5", "7", "zoo", "9", "zoo", "2", "4", "6", "apple", "8", "10"]
sortInPlace()方法无返回值,会改变原数组的值:
stringList1.sortInPlace() print("stringList1: \(stringList1)")
运行结果:
stringList1: ["1", "10", "2", "3", "4", "5", "6", "7", "8", "9", "apple", "zoo", "zoo"]
+=运算符只能用于数组之间,不能添加单个元素:
var str = "myString" stringList1 += str //此行报错:Binary operator ‘+=‘ cannot be applied to operands of type ‘[String]‘ and ‘String‘
以上是关于Swift中常见的String用法,Array高阶使用,Set集合操作的主要内容,如果未能解决你的问题,请参考以下文章
Swift学习Array学习深入浅出Swift高阶函数-MapFlatMapCompactMapCompactMapValuesFilterReduce