哈哈哈,剛才看見(jiàn)微博,有人推薦一個(gè)神奇的工具,推薦給大家http://swiftlang.ng.bluemix.net/#/repl是IBM公司出的,編譯器的版本是Swift2.2,現(xiàn)在可以使用之前的String的那些方法了,哈哈哈
插入和刪除(Inserting and Removing)
調(diào)用insert(_:atIndex:)方法可以在一個(gè)字符串的指定索引插入一個(gè)字符
var welcome = "hello"
welcome.insert("!",atIndex:welcome.endIndex)
//welcome現(xiàn)在就是"hello!"
還可以在一個(gè)字符串的指定索引插入一個(gè)字符串,調(diào)用insertContentsOf(_:at:)方法
welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor())
現(xiàn)在welcome就是"hello there!"(是接著上面的例子哦,暫時(shí)的編譯器還是會(huì)報(bào)錯(cuò)的,待升級(jí)之后再試試)
調(diào)用removeAtIndex(_:) 方法可以在一個(gè)字符串的指定索引刪除一個(gè)字符
var welcome = "hello there!"
welcome.removeAtIndex(welcome.endIndex.predecessor())
//這時(shí)候welcome的值就是"hello there",把最后一個(gè)字符后一位的前一位移除了就是移除了"!"
調(diào)用removeRange(_:)方法可以在一個(gè)字符串指定索引刪除一個(gè)子字符串
var welcome = "hello there"
let range = welcome.endIndex.advanceBy(-6)..<welcome.endIndex
welcome.removeRange(range)
DAY2里面說(shuō)過(guò),我們可以調(diào)用advanceBy()來(lái)獲取一個(gè)索引,編譯器的原因暫時(shí)不能實(shí)現(xiàn),上面的例子表示的意思是從空格刪除到最后的e,welcome現(xiàn)在就等于hello
比較字符串(Comparing Strings)
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 string are considered equal")
}
//These two string are considered equal
我們可以使用(==)和(!=)來(lái)比較,如果兩個(gè)字符串的可擴(kuò)展的字形群集是標(biāo)準(zhǔn)相等的,那么它們就是相等的,即使可擴(kuò)展的字形群集是由不同的Unicode標(biāo)量構(gòu)成的,只要語(yǔ)言意義和外觀相同就認(rèn)為是相同的.
let a = "caf\u{E9}" //é
let b = "caf\u{65}\u{301}" //e和重音符號(hào)
if a == b{
print("These two strings are considered equal")
}
//These two strings are considered equal
前綴/后綴相等(Prefix and Suffix Equality )
通過(guò)調(diào)用字符串hasPrefix()/hasSuffix()方法來(lái)檢查是否有前后綴,兩個(gè)都輸入一個(gè)String類型的參數(shù),返回一個(gè)布爾值。這里舉了一個(gè)例子,用一個(gè)定義為常量的數(shù)組romeoAndJuliet,可以看見(jiàn),數(shù)組是用[]來(lái)申明的,數(shù)組內(nèi)的變量用逗號(hào)隔開,具體的數(shù)據(jù)類型我看后面有詳細(xì)的章節(jié)會(huì)講,到時(shí)候?qū)W習(xí)
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"
]
定義了一個(gè)變量用來(lái)計(jì)數(shù)前綴中含有Act 1的,這里一共有五次
var act1SceneCount = 0
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1"){
++act1SceneCount
}
}
print("There are \(act1SceneCount) scenes in Act 1")
//There are 5 scenes in Act 1
定義了兩個(gè)變量,來(lái)數(shù)字符串后綴中含有這兩個(gè)的值,這里有一個(gè)for循環(huán)一個(gè)if,else if循環(huán),具體的語(yǔ)法后面也有詳細(xì)的章節(jié)講解
var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet{
if scene.hasSuffix("Capulet's mansion"){
++mansionCount
}else if scene.hasSuffix("Friar Lawrence's cell"){
++cellCount
}
}
print("\(mansionCount) mansion scenes;\(cellCount) cell scenes")
//6 mansion scenes;2 cell scenes
Unicode Representations of Strings
apple的String&Character中還有這個(gè)章節(jié)Unicode Representations of Strings,這里就不示范了,有興趣的大家可以看一下,我睡覺(jué)前看下~