? ? Swift提供了3種方式去比較文本值:比較字符串和字符相等,比較前綴相等搭伤,比較后綴相等。
比較字符串和字符相等
????比較字符串和字符相等可以使用相等運(yùn)算符(==)和不相等運(yùn)算符(!=):
????????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
? ? 2個(gè)字符串或者2個(gè)字符,如果它們的字形集是按照一定規(guī)則相等的,就可以認(rèn)為它們是相等的阳谍。字形集是按照一定規(guī)則相等是指它們有一樣的語(yǔ)言意義和外觀,即使它們其實(shí)由不同的Unicode標(biāo)量組成螃概。
? ? 比如標(biāo)量LATIN SMALL LETTER E WITH ACUTE (U+00E9)和LATIN SMALL LETTER E (U+0065)加上COMBINING ACUTE ACCENT (U+0301)就可以被認(rèn)為是一定定義上相等的矫夯。它們的字形集都可以表示字符é,所以它們被認(rèn)為是一定規(guī)則上的相等吊洼。如下:
? ??????// ?LATIN SMALL LETTER E WITH ACUTE
????????let eAcuteQuestion = "Voulez-vous un caf\u{E9}?" ?//?Voulez-vous un café?
????????// using LATIN SMALL LETTER E and COMBINING ACUTE ACCENT
????????let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?" //Voulez-vous un café?
????????if eAcuteQuestion == combinedEAcuteQuestion {
????????? ? print("These two strings are considered equal")
????????}
????????// 打印 "These two strings are considered equal”
? ? 相反的训貌,英語(yǔ)國(guó)家使用的LATIN CAPITAL LETTER A (U+0041, or "A")和俄語(yǔ)國(guó)家使用的CYRILLIC CAPITAL LETTER A (U+0410, or "А")就被認(rèn)為是不相等的。這2個(gè)字符看起來(lái)很像冒窍,但是它們的語(yǔ)意是不一樣的:
? ??????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.”
NOTE:Swift中的字符串和子符的比較不是地點(diǎn)敏感的递沪。
前綴相等和后綴相等
? ? 判斷字符串是不是有特定的前綴或者后綴豺鼻,可以使用方法hasPrefix(_:)和hasSuffix(_:)。2個(gè)方法的入?yún)⒍际且粋€(gè)字符串区拳,然后返回布爾值拘领。
? ? 下面的例子中是一個(gè)包含了字符串的數(shù)組,里面是莎士比亞的羅密歐和朱麗葉的前兩個(gè)場(chǎng)景:
? ??????????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(_:)計(jì)算出數(shù)組romeoAndJuliet中第一個(gè)場(chǎng)景的數(shù)量:
? ??????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”
????相似的樱调,也開(kāi)始使用方法hasSuffix(_:)計(jì)算出發(fā)生在Capulet’s mansion和Friar Lawrence’s cel的場(chǎng)景:????
? ??????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")
????????// Prints "6 mansion scenes; 2 cell scenes”
NOTE:方法hasPrefix(_:)和hasSuffix(_:)對(duì)每一個(gè)字符串的字形集按照上面提的一定規(guī)則得相等進(jìn)行逐字比較。