Swift--字符串和字符(二)
Swift的字符串類型表示的字符值以特定順序的集合。每個字符的值代表一個Unicode字符。您可以通過遍歷該字符串用for-in循環訪問字符串中的單個字符值:
for character in "Dog!"{
println(character)
}?//D 5 //o 6 //g 7 //! 8 //
創建一個單獨的字符串常量或者變量可以通過一個字符類型的注釋來完成:
let yenSign: Character = "¥"
?
字符計數
?
如果我們想要計算一個字符串里有多少字符,我們可以通過countElements函數來實現
.??? let unusualMenagerie= "Koala , Snail ,Penguin , Dromedary "
.??? ?println("unusualMenagerie has \(countElements(unusualMenagerie))characters")
.??? ??// prints "unusualMenagerie has 40 characters"
?
字符和字符串的拼接
字符串和字符的拼接可以通過(+)來實現
??var instruction= "look over"
?instruction+= string2
//instruction now equals "look over there
??var welcome= "good morning"
?welcome+= character1
??// welcomenow equals "good morning!"
?
字符串插入
字符串插值是一種由包括他們在字符串中的值從字面常量,變量,常量和表達式的組合構造一個新的String值。您插入字符串字面量中的每一項被包裹在一對圓括號,以反斜線前綴:
.??? let multiplier = 3
??let message = "\(multiplier)times 2.5 is \(Double(multiplier) * 2.5)"
??// message is "3 times 2.5 is 7.5"
在上面的例子中,乘數的值插入到一個字符串作為\(multiplier)。此占位符替換為乘數當字符串插值計算來創建一個實際的字符串的實際值。
乘數的值也是一個更大的表達式后的字符串中的一部分。該表達式計算雙(multiplier)*2.5的值,并將結果插入(7.5)到字符串中。在這種情況下,表達式寫為\(雙人間(multiplier)*2.5),當它被包含在里面的字符串文字。
?
字符串比較
Swift提供了三種方法來比較兩個字符串值:字符串相等,前綴相等和后綴相等。
字符串相等:
.??? let quotation = "We're a lot alike, you and I."
.??? ?let sameQuotation = "We're a lot alike, you and I."
.??? ??if quotation == sameQuotation {
.??? println("These two strings are considered equal")
}?//prints "These two strings are considered equal"
要檢查一個字符串是否有一個特定的字符串前綴或后綴,可使用字符串的hasPrefix和hasSuffix方法,這兩種接受一個類型為String的參數,并返回一個布爾值。這兩種方法執行的基本字符串和前綴或后綴字符串之間的字符逐個字符比較。
下面的例子中考慮代表從莎士比亞的羅密歐與朱麗葉的第一個兩幕場景位置的字符串數組:
.??? 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'smansion",
.??? ??"Act 1 Scene 5: The Great Hall in Capulet'smansion",
.??? ??"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"
.??? ? ]
您可以使用romeoAndJuliet的hasPrefix方法:
var act1SceneCount = 0
?
for scene in romeoAndJuliet {
??? if scene.hasPrefix("Act 1 ") {
??????? ++act1SceneCount
??? }
}?println("Thereare \(act1SceneCount) scenes in Act 1")
// prints "There are 5 scenes in Act 1"
?
同理, hasSuffix方法如下
var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
if scene.hasSuffix("Capulet'smansion") {
++mansionCount?}
else if scene.hasSuffix("FriarLawrence's cell") {
++cellCount}
}
.??? ??println("\(mansionCount) mansionscenes; \(cellCount) cell scenes")
.??? ??// prints "6 mansion scenes; 2 cell scenes"
?
字符串大寫和小寫
你可以通過一個字符串的uppercaseString和lowercaseString屬性來實現大寫或小寫:
.??? ?let normal = "Couldyou help me, please?"
.??? ??let shouty = normal.uppercaseString
.??? ??// shouty is equal to "COULD YOU HELP ME,PLEASE?"
.??? ??let whispered = normal.lowercaseString
.??? ??// whispered is equal to "could you help me,please?"
?
總結
以上是生活随笔為你收集整理的Swift--字符串和字符(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荣耀 CEO 赵明:2023 年产品相较
- 下一篇: 华为公司已起诉小米专利侵权 国家知识产权