Swift でテキスト値を比較する方法には、文字列および文字の一致、前方一致、および後方一致の 3 つがあります。

文字列および文字の一致

文字列および文字の等式は、Comparison Operators で説明されているように、「等価」演算子 (==) と「不等価」演算子 (!=) を使用します。

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 つの String 値(または Character 値)が等価であるとみなされます。異なる Unicode スカラで構成されている場合でも、言語的な意味と出現が同じ場合には、拡張書記素クラスタは正準等価となります。

例として、LATIN SMALL LETTER E WITH ACUTE (U+00E9) は、LATIN SMALL LETTER E (U+0065) に続けて COMBINING ACUTE ACCENT (U+0301) と正準等価になります。これらの拡張書記素クラスタは共に、文字 é を表現する有効な手段で、正準等価であるとみなされます。

// "Voulez-vous un café?" は LATIN SMALL LETTER E WITH ACUTE を使用
let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"

// "Voulez-vous un café?" は LATIN SMALL LETTER E と COMBINING ACUTE ACCENT を使用
let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?"

if eAcuteQuestion == combinedEAcuteQuestion {
    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 または "А") と等しくありません。文字の見た目は似ていますが、言語的に意味が異なります。

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 での文字列および文字の比較は、ロケールに対応していません。

前方一致と後方一致

文字列が特定の接頭辞または接尾辞を持つことを調べるためには、String 型の引数を 1 つ取り、ブール値を返す hasPrefix(_:) メソッドと hasSuffix(_:) メソッドを呼び出します。

次の例は、シェイクスピアの『ロミオとジュリエット』の初めの 2 幕の場面を表現する文字列の配列です。

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"
]

劇の第 1 幕にある場面数をカウントするために、romeoAndJuliet 配列の 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" と出力

同様に、Capulet’s mansion と Friar Lawrence’s cell の場面数をカウントするために、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" と出力
NOTE
hasPrefix(_:) と hasSuffix(_:) メソッドは、String and Character Equality で説明されているように、拡張書記素クラスタを文字ごとに正準等価の比較を行います。

Portions of this page are translations based on work created and shared by Apple and used according to terms described in the Creative Commons Attribution 4.0 International License.