日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Swift--字符串和字符(二)

發(fā)布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Swift--字符串和字符(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Swift的字符串類型表示的字符值以特定順序的集合。每個字符的值代表一個Unicode字符。您可以通過遍歷該字符串用for-in循環(huán)訪問字符串中的單個字符值:

for character in "Dog!"{

println(character)

}?//D 5 //o 6 //g 7 //! 8 //

創(chuàng)建一個單獨的字符串常量或者變量可以通過一個字符類型的注釋來完成:

let yenSign: Character = "¥"

?

字符計數(shù)

?

如果我們想要計算一個字符串里有多少字符,我們可以通過countElements函數(shù)來實現(xiàn)

.??? let unusualMenagerie= "Koala , Snail ,Penguin , Dromedary "

.??? ?println("unusualMenagerie has \(countElements(unusualMenagerie))characters")

.??? ??// prints "unusualMenagerie has 40 characters"

?

字符和字符串的拼接

字符串和字符的拼接可以通過(+)來實現(xiàn)

??var instruction= "look over"

?instruction+= string2

//instruction now equals "look over there

??var welcome= "good morning"

?welcome+= character1

??// welcomenow equals "good morning!"

?

字符串插入

字符串插值是一種由包括他們在字符串中的值從字面常量,變量,常量和表達式的組合構(gòu)造一個新的String值。您插入字符串字面量中的每一項被包裹在一對圓括號,以反斜線前綴:

.??? let multiplier = 3

??let message = "\(multiplier)times 2.5 is \(Double(multiplier) * 2.5)"

??// message is "3 times 2.5 is 7.5"

在上面的例子中,乘數(shù)的值插入到一個字符串作為\multiplier)。此占位符替換為乘數(shù)當(dāng)字符串插值計算來創(chuàng)建一個實際的字符串的實際值。
乘數(shù)的值也是一個更大的表達式后的字符串中的一部分。該表達式計算雙(multiplier*2.5的值,并將結(jié)果插入(7.5)到字符串中。在這種情況下,表達式寫為\(雙人間(multiplier*2.5),當(dāng)它被包含在里面的字符串文字。

?

字符串比較

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的參數(shù),并返回一個布爾值。這兩種方法執(zhí)行的基本字符串和前綴或后綴字符串之間的字符逐個字符比較。
下面的例子中考慮代表從莎士比亞的羅密歐與朱麗葉的第一個兩幕場景位置的字符串?dāng)?shù)組:

.??? 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屬性來實現(xiàn)大寫或小寫:

.??? ?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?"

?

總結(jié)

以上是生活随笔為你收集整理的Swift--字符串和字符(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。