Swift中String代表字符串類型。Swift中的"String"類型無縫橋接了Foundation框架中的"NSString"類型。
初始化空的String(Initializing an Empty String)
var emptyString = ""
var anotherEmptyString = String()
判斷String是否為空,可以通過布爾類型的"isEmpty"屬性:
if emptyString.isEmpty {
print("Nothing to see here")
}
//Prints "Nothing to see here"
字符串是值類型(Strings Are Value Types)
Swift 默認(rèn)字符串拷貝的方式保證了在函數(shù)/方法中傳遞的是字符串的值,其明確了無論該值來自于哪里,都是你獨自擁有的。你可以放心你傳遞的字符串本身不會被更改。
在實際編譯時,Swift編譯器會優(yōu)化字符串的使用,使實際的復(fù)制只發(fā)生在絕對必要的情況下,這意味著你始終可以將字符串作為值類型的同時獲得極高的性能。
使用字符(Working with Characters)
for characters in "Dog!??".characters {
print(characters)
}
// D
// o
// g
// !
// ??
String可以通過Character數(shù)組初始化得到
字符串的插入(String Interpolation)
Unicode
Unicode標(biāo)量(Scalars)
Swift的String類型就在Unicode標(biāo)量的基礎(chǔ)上建立的.每一個字符或者修飾符(modifier)都是獨一無二的21位數(shù)的Unicode標(biāo)量。??:"U+0061"就是小寫的拉丁字母A("a"),或者U+1F425就是正面的小雞("??")。
U+0061 for LATIN SMALL LETTER A ("a"), or U+1F425 for FRONT-FACING BABY CHICK ("??").
不是所有的Unicode變量都對應(yīng)一個字符,有可能是預(yù)留出來的。每個Unicode標(biāo)量都有一個自己的名字,參考上面??。
字符串字面量中的特殊字符(Special Characters in String Literals)
字符串字面量中可以包括下面這些特殊的字符:
- 轉(zhuǎn)義字符
\0 (空字符,null character)
\\ (反斜杠,backslash)
\t (水平制表符,horizontal tab)
\n (換行符,line feed)
\r (回車,carriage return)
\" (雙引號,double quote)
\' (單引號,single quote)
-
任意的Unicode標(biāo)量,寫法是"\u{n}",其中n是1-8位的16進(jìn)制數(shù),代表一個有效的Unicode代碼點。
??
計算字符個數(shù)(Counting Characters)
為了得到一個字符串中字符的數(shù)量,可以使用字符串的"characters"屬性的"count"屬性:
let unusualMenagerie = "Koala ??, Snail ??, Penguin ??, Dromedary ??"
print("unusualMenagerie has \(unusualMenagerie.characters.count) characters")
// Prints "unusualMenagerie has 40 characters"
Swift在字符的值上使用的擴(kuò)展字符集群,也就是說,在某些連接、修改的情況下,不會出現(xiàn)字符串長度的變化。
??:
var word = "cafe"
print("the number of characters in \(word) is \(word.characters.count)")
// Prints "the number of characters in cafe is 4"
word += "\u{301}" // COMBINING ACUTE ACCENT, U+0301
print("the number of characters in \(word) is \(word.characters.count)")
// Prints "the number of characters in cafe? is 4"
得到和修改字符串(Accessing and Modifying a String)
可以通過String的方法、屬性或者使用下標(biāo)語法。
字符串索引值(String Indices)
不同的字符需要不同大小的內(nèi)存來進(jìn)行存儲,所以為了決定Character在一個特殊的位置,你就必須要從一個字符串的起始到終止位置重復(fù)每一個Unicode標(biāo)量。因為這個原因,Swift的字符串不能用整型值來索引。
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a
插入和移除(Inserting and Removing)
var welcome = "hello"
welcome.insert("!", at: welcome.endIndex)
// welcome now equals "hello!"
welcome.insert(contentsOf:" there".characters, at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there!"
welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome now equals "hello"
字符串的比較(Comparing Strings)
字符串和字符相等(String and Character Equality)
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")
}
// Prints "These two strings are considered equal"
如果兩個字符串值(字符值)得擴(kuò)展字符集群具有統(tǒng)一碼等價性(canonically equivalent),那么就被認(rèn)為是相等的。
??:
// "Voulez-vous un café?" using LATIN SMALL LETTER E WITH ACUTE
let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"
// "Voulez-vous un cafe??" using 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")
}
// Prints "These two strings are considered equal"
但是存在下面這種情況,雖然看起來是一樣的,但是因為Unicode標(biāo)量不同,所以不相等:
let latinCapitalLetterA: Character = "\u{41}"
//斯拉夫字母A,用于俄語當(dāng)中
let cyrillicCapitalLetterA: Character = "\u{0410}"
if latinCapitalLetterA != cyrillicCapitalLetterA {
print("These two characters are not equivalent.")
}
// Prints "These two characters are not equivalent."
前綴/后綴相等(Prefix and Suffix Equality)
類似的,用"hasSuffix(_:)"方法來判斷結(jié)尾處。