美文网首页
Swift----String、Array、Dictionary

Swift----String、Array、Dictionary

作者: 彬至睢阳 | 来源:发表于2018-11-11 10:16 被阅读0次

//初始化一个字符串-并赋值为空

        letempty =""

        letempty1 =String()

        print(empty)

        print(empty1)

在字符串中包含值有一种更简单的方法:将值写在圆括号中,并在圆括号前写反斜杠(\)。例如:

letapples=3

letoranges=5

letappleSummary="I have \(apples) apples."

letfruitSummary="I have \(apples+oranges) pieces of fruit."

        //去除一个字符串中的特定字符

        let str = "The rain in Spain stays mainly in the plain."

        letvowels:Set = ["a","e","i","o","u"]

        letdisemvoweled =String(str.lazy.filter{ !vowels.contains($0) })//swift自动为闭包提供参数名缩写功能,可以直接通过$0和$1等来表示闭包中的第一个第二个参数,并且对应的参数类型会根据函数类型来进行判断

        print(disemvoweled)

        print(str.hasPrefix("123"))//获取字符的前缀与123字符比较

         print(str.hasSuffix("123"))//获取字符的后缀与123字符比较

        //初始化一个字符串并且赋值为ab重复10次的字符

        lets =String(repeating:"ab", count:10)

        print(s.isEmpty)//字符是否为空

        print(s.count)//字符的个数

        print(s.lowercased())//输出小写字符

         print(s.uppercased())//输出大写字符

        print("\(s.isEmpty) ---\(s.count)")

        print(s)

        letp =CGPoint.init(x:0, y:50)

        print(String(describing: p))

         print(String(reflecting: p))

        //去除-

        varnonempty ="non-empty"

        ifleti = nonempty.firstIndex(of:"-") {

            nonempty.remove(at: i)

        }

        print(nonempty)

        //去除特定字符

        var phrase = "The rain in Spain stays mainly in the plain."

        letvowels1:Set = ["a","e","i","o","u"]

        phrase.removeAll(where: { vowels1.contains($0) })

        var bugs = ["Aphid", "Bumblebee", "Cicada", "Damselfly", "Earwig"]

        bugs.removeFirst(3)//去除数组前三个元素

        print(bugs)

        // Prints "["Damselfly", "Earwig"]"

        let cast = ["Vivien", "Marlon", "Kim", "Karl"]

        letshortNames = cast.filter{ $0.count<5}

        print(shortNames)

        // Prints "["Kim", "Karl"]"

        //判断一个数组中是否有指定元素

        let cast1 = ["Vivien", "Marlon", "Kim", "Karl"]

        print(cast1.contains("Marlon"))

        //将数组中指定的元素改为另外的元素

        varstudents = ["Ben","Ivy","Jordell","Maxime"]

        ifleti = students.firstIndex(of:"Maxime") {

            students[i] ="Max"

        }

        print(students)

        //输出字典中中值最大的

        let hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]

        letgreatestHue = hues.max{ a, bina.value < b.value }

        print(greatestHue)

        //将字符串以空格为界限变为数组

        let line = "BLANCHE:  I don't want realism. I want magic!"

        print(line.split(separator:" "))

        //将字符串以空格为界限变为数组

        let line1 = "BLANCHE:  I don't want realism. I want magic!"

        print(line1.split(whereSeparator: { $0 ==" "}))

//创建一个整数数组

        varemptyArray =Array()

        print(emptyArray.count)//数组的元素个数

        print(emptyArray.capacity)//数组的容量

        print(emptyArray.isEmpty)//数组是否为空

       letnumbers = [10,20,30,40,50]

        print(numbers.first)//数组的第一个元素

        print(numbers.last)//数组的第后个元素

        print(numbers.endIndex)//数组的结束位置---

        print(numbers.startIndex)//数组的开始位置---0

        print(numbers[numbers.startIndex])

        print(numbers[numbers.endIndex-1])

        // print(numbers[numbers.endIndex])//这样是不对的,虽然数组的结束位置为5,但是最后一个元素的位置是(numbers.endIndex-1)

        print(numbers)

        vararray = [10,20,30]

        array.append(40)//向数组中添加一个元素

        array.insert(50, at: array.endIndex)//向数组指定位置插入一个元素

        array.insert(contentsOf:100...105, at: array.endIndex)//向数组指定位置插入一个集合

        array.replaceSubrange(1...3, with:repeatElement(1, count:5))//替换数组位置1、2、3的t元素

        array.remove(at:3)//移除数组指定位置的元素

        array.removeFirst()////移除数组第一个元素

        array.removeLast()////移除数组最后一个元素

        array.removeAll(where: { $0 %2==1})//根据指定条件删除元素

        array.removeFirst(3)////移除数组前三个元素

        array.removeLast(3)////移除数组最后三个元素

        array.removeSubrange(1...3)//移除数组指定范围的元素

        array.removeAll()////移除数组中的所有元素

        print(array.contains(10))//判断数组是否包含某个元素

    lethasBigPurchase = array.contains{ $0 >100}//获取数组中满足某个条件的元素并形成一个新数组

        //数组中满足条件的元素组成一个新数组

        let cast12 = ["Vivien", "Marlon", "Kim", "Karl"]

        letshortNames1 = cast12.filter{ $0.count<5}

        print(shortNames1)

 //遍历数组

        forwordincast12 {

            print(word)

        }

        cast.forEach { (word)in

            print(word)

        }

 //遍历数组--是否包含大于100的数

        letexpenses = [21.37,55.21,9.32,10.18,388.77,11.41]

        lethasBigPurchase1 = expenses.contains{ $0 >100}

 //遍历数组--数组中的元素所含字符个数是否全都大于5

        let names = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]

        letallHaveAtLeastFive = names.allSatisfy({ $0.count>=5})

        // allHaveAtLeastFive == true

//求数组元素的和

letnumbers = [1,2,3,4]letnumberSum = numbers.reduce(0, { x, yinx + y})// numberSum == 10

//------------------字典---------------//

        vardict = ["a":1,"b":2]

        //保u存key--a原来的值

       dict.merge(["a":3,"c":4]) { (current,_)incurrent }

       dict.merge(zip(["a","c"], [3,4])) { (current,_)incurrent }

        // ["b": 2, "a": 1, "c": 4]

        //取代key---a原来的值

        dict.merge(["a":5,"d":6]) { (_, new)innew }

        dict.merge(zip(["a","d"], [5,6])) { (_, new)innew }

        // ["b": 2, "a": 5, "c": 4, "d": 6]

        //移除值--6

        print(dict.removeValue(forKey:"a"))

        //字典的遍历

        let hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]

        for(name, hueValue)inhues {

            print("The hue of \(name) is \(hueValue).")

        }

        // Prints "The hue of Heliotrope is 296."

        // Prints "The hue of Coral is 16."

        // Prints "The hue of Aquamarine is 156."

        //输出字典中值最小的

        let hues1 = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]

        letleastHue = hues1.min{ a, bina.value < b.value }

        print(leastHue)

        // Prints "Optional(("Coral", 16))"

        //输出字典中值最大的

        let hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]

        letgreatestHue = hues.max{ a, bina.value < b.value }

        print(greatestHue)

        // Prints "Optional(("Heliotrope", 296))"

相关文章

网友评论

      本文标题:Swift----String、Array、Dictionary

      本文链接:https://www.haomeiwen.com/subject/yagwtqtx.html