? ? Swift提供了3種方式去比較文本值:比較字符串和字符相等,比較前綴相等,比較后綴相等。
比較字符串和字符相等
????比較字符串和字符相等可以使用相等運算符(==)和不相等運算符(!=):
????????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個字符串或者2個字符,如果它們的字形集是按照一定規(guī)則相等的,就可以認為它們是相等的。字形集是按照一定規(guī)則相等是指它們有一樣的語言意義和外觀,即使它們其實由不同的Unicode標量組成。
? ? 比如標量LATIN SMALL LETTER E WITH ACUTE (U+00E9)和LATIN SMALL LETTER E (U+0065)加上COMBINING ACUTE ACCENT (U+0301)就可以被認為是一定定義上相等的。它們的字形集都可以表示字符é,所以它們被認為是一定規(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”
? ? 相反的,英語國家使用的LATIN CAPITAL LETTER A (U+0041, or "A")和俄語國家使用的CYRILLIC CAPITAL LETTER A (U+0410, or "А")就被認為是不相等的。這2個字符看起來很像,但是它們的語意是不一樣的:
? ??????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中的字符串和子符的比較不是地點敏感的。
前綴相等和后綴相等
? ? 判斷字符串是不是有特定的前綴或者后綴,可以使用方法hasPrefix(_:)和hasSuffix(_:)。2個方法的入?yún)⒍际且粋€字符串,然后返回布爾值。
? ? 下面的例子中是一個包含了字符串的數(shù)組,里面是莎士比亞的羅密歐和朱麗葉的前兩個場景:
? ??????????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(_:)計算出數(shù)組romeoAndJuliet中第一個場景的數(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”
????相似的,也開始使用方法hasSuffix(_:)計算出發(fā)生在Capulet’s mansion和Friar Lawrence’s cel的場景:????
? ??????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(_:)對每一個字符串的字形集按照上面提的一定規(guī)則得相等進行逐字比較。