Swift5.1—比較字符串

Swift提供了三種方式來比較文本值:字符串字符相等、前綴相等和后綴相等。

字符串/字符相等

字符串/字符可以用等于操作符(==)和不等于操作符(!=)。

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”

如果兩個(gè)字符串(或者兩個(gè)字符)的可擴(kuò)展的字形群集是標(biāo)準(zhǔn)相等,那就認(rèn)為它們是相等的。只要可擴(kuò)展的字形群集有同樣的語言意義和外觀則認(rèn)為它們標(biāo)準(zhǔn)相等,即使它們是由不同的 Unicode 標(biāo)量構(gòu)成。

例如,LATIN SMALL LETTER E WITH ACUTE(U+00E9)就是標(biāo)準(zhǔn)相等于LATIN SMALL LETTER E(U+0065)后面加上COMBINING ACUTE ACCENT(U+0301)。這兩個(gè)字符群集都是表示字符é的有效方式,所以它們被認(rèn)為是標(biāo)準(zhǔn)相等的。

//"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 and 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,或者 A)。兩個(gè)字符看著是一樣的,但卻有不同的語言意義。

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”

注:在Swift中,字符串和字符并不區(qū)分地域(not locale-sensitive)。

**前綴/后綴相等**

通過調(diào)用字符串的hasPrefix(_:)/hasSuffix(_:)方法來檢查字符串是否擁有特定前綴/后綴,兩個(gè)方法均接收一個(gè)String類型的參數(shù),并返回一個(gè)布爾值。

下面的例子以一個(gè)字符串?dāng)?shù)組表示莎士比亞話劇《羅密歐與朱麗葉》中前兩場(chǎng)的場(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"
]


你可以調(diào)用hasPrefix(_:)方法來計(jì)算話劇中第一幕的場(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”


相似地,你可以用hasSuffix(_:)方法來計(jì)算發(fā)生在不同地方的場(chǎng)景數(shù)。

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; 1 cell scenes”

注:hasPrefix(:)和hasSuffix(:)方法都是在每個(gè)字符串中逐字符比較其可擴(kuò)展的字符群集是否標(biāo)準(zhǔn)相等。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容