美文网首页
iOS开发 - 「Swift 学习」String字符、前缀/后缀

iOS开发 - 「Swift 学习」String字符、前缀/后缀

作者: 俺不是大佬儿 | 来源:发表于2021-11-16 17:35 被阅读0次

    Swift — String比较

    Swift 提供了三种方式来比较文本值:字符串字符相等、前缀相等和后缀相等

    一、字符相等

    如果两个字符串(或者两个字符)的可扩展的字形群集是标准相等的,那就认为它们是相等的。在这个情况下,即使可扩展的字形群集是有不同的 Unicode 标量构成的,只要它们有同样的语言意义和外观,就认为它们标准相等。(例如,LATIN SMALL LETTER E WITH ACUTE(U+00E9)就是标准相等于LATIN SMALL LETTER E(U+0065)后面加上COMBINING ACUTE ACCENT(U+0301)。这两个字符群集都是表示字符é的有效方式,所以它们被认为是标准相等的:)

            let quotation = "We're a lot alike, you and I."
            let sameQuotation = "We're a lot alike, you and I."
            if quotation == sameQuotation {
                
                print("These two strings are considered equal")
                
            }
            // 打印输出 "These two strings are considered equal"
            
            
            //相反,英语中的LATIN CAPITAL LETTER A(U+0041,或者A)不等于俄语中的CYRILLIC CAPITAL LETTER A(U+0410,或者A)。两个字符看着是一样的,但却有不同的语言意义:
            
            let latinCapitalLetterA: Character = "\u{41}"
            
            let cyrillicCapitalLetterA: Character = "\u{0410}"
            
            if latinCapitalLetterA != cyrillicCapitalLetterA {
                
                print("These two characters are not equivalent")
            }
            // 打印 "These two characters are not equivalent"
    
    

    二、前缀/后缀相等

           //前缀相等
            let romeoAndJuliet = [
                "Act 1 Scene 1: Verona, A public place",
                "Act 1 Scene 2: Capulet's mansion",
                "Act 1 Scene 3: A room in Capulet's mansion",
                "Act 1 Scene 4: A street outside Capulet's mansion",
                "Act 1 Scene 5: The Great Hall in Capulet's mansion",
                "Act 2 Scene 1: Outside Capulet's mansion",
                "Act 2 Scene 2: Capulet's orchard",
                "Act 2 Scene 3: Outside Friar Lawrence's cell",
                "Act 2 Scene 4: A street in Verona",
                "Act 2 Scene 5: Capulet's mansion",
                "Act 2 Scene 6: Friar Lawrence's cell"
            ]
            
            //可以调用hasPrefix(_:)方法来计算话剧中第一幕的场景数:(遍历查找前缀相等)
            var act1SceneCount = 0
            for scene in romeoAndJuliet {
               
                if scene.hasPrefix("Act 1 ") {
                    act1SceneCount += 1
                }
            }
            
            print("There are \(act1SceneCount) scenes in Act 1")
            // 打印输出 "There are 5 scenes in Act 1"
            
    
            //后缀相等
            //可以用hasSuffix(_:)方法来计算发生在不同地方的场景数:(后缀相等)
            var mansionCount = 0
            var cellCount = 0
            for scene in romeoAndJuliet {
    
                if scene.hasSuffix("Capulet's mansion") {
                    mansionCount += 1
                } else if scene.hasSuffix("Friar Lawrence's cell") {
                    cellCount += 1
                }
            }
            print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")
            // 打印输出 "6 mansion scenes; 2 cell scenes"
    

    hasPrefix(_:)hasSuffix(_:)方法都是在每个字符串中逐字符比较其可扩展的字符群集是否标准相等

    \color{gray}{欢迎大佬儿来指正纠错,共同学习😏!!}

    相关文章

      网友评论

          本文标题:iOS开发 - 「Swift 学习」String字符、前缀/后缀

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