字元和字串
字串常量可以包括下面這些特殊字元:
空字元\0,反斜槓\,製表符\t,換行符\n,回車符\r,雙引號\」和單引號\』
單位元組unicode字元,\xnn,其中nn是兩個十六進製制數
雙位元組unicode字元,\unnnn,其中nnnn是四個十六進製制數
四位元組unicode字元,\unnnnnnnn,其中nnnnnnnn是八個十六進製制數
2. 初始化乙個空串(兩種)
var emptystring = ""
if emptystring.isempty
//nothing to see here
var emptystring = string()
if emptystring.isempty
//is empty
3. 變長字串
var variablestring = "horse"
variablestring += " and carriage"
println(variablestring)
//horse and carriage
4.
字串不是指標,而是實際的值
在swift中,乙個string型別就是乙個實際的值,當定義乙個新的string,並且將之前的string值拷貝過來的時候,是實際建立了乙個相等的新值,而不是僅僅像指標那樣指向過去。
同樣在函式傳遞引數的時候,也是傳遞的實際值,並且建立了乙個新的字串,後續的操作都不會改變原有的string字串。
5. 字元
swift的字串string就是由字元character組成的,每乙個character都代表了乙個特定的unicode字元。通過for-in迴圈,可以遍歷字串中的每乙個字元:
for character in "****"
//f//u
//c//k
定義乙個單獨的字元:
let oneword : character = "s"
println(oneword)
//s
6. 字元計數
使用全域性函式countelements可以計算乙個字串中字元的數量:
println("str has \(countelements(str)) character")
//str has 19 character
乙個空格也算乙個字元。
7. 組合使用字元和字串
string和character型別可以通過使用+號相加來組合成乙個新的字串
let string1 = "hello"
let string2 = " there"
let character1: character = "!"
let character2: character = "?"
let stringpluscharacter = string1 + character1
println(stringpluscharacter) //hello!
let stringplusstring = string1 + string2
println(stringplusstring) //hello there
let characterpluscharacter = character1 + character2
println(characterpluscharacter)//!?
let characterplusstring = character1 + string1
println(characterplusstring) //!hello
也可以使用+=號來組合
var instruction = "look over"
let string2 = " there"
instruction += string2
println(instruction) //look over there
8. 使用字串生成新串
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(double(multiplier) * 2.5)"
println(message) // 3 times 2.5 is 7.5
9. 字串比較
swift提供三種方法比較字串的值:字串相等,字首相等,和字尾相等
字串相等:
當兩個字串的包含完全相同的字元時,他們被判斷為相等。
let quotation = "i knew you were trouble"
let samequotation = "i knew you were trouble"
if quotation == samequotation
// these two strings are considered equal
字首(hasprefix)相等和字尾(hassuffix)相等:
使用string 類的兩個方法hasprefix和hassuffix,來檢查乙個字串的字首或者字尾是否包含另外乙個字串,它需要乙個string型別型的引數以及返回乙個布林型別的值。兩個方法都會在原始字串和字首字串或者字尾字串之間做字元與字元之間的。
let quotation = "i knew you were trouble"
let prefiquotation = "i" // knew you were trouble
if quotation.hasprefix(prefiquotation)
//i is prefix
let quotation = "i knew you were trouble"
let suffixquotation = "trouble" // knew you were trouble
if quotation.hassuffix(suffixquotation)
// trouble is suffix
let quotation = "i knew you were trouble"
let suffixquotation = "i" // knew you were trouble
if quotation.hassuffix(suffixquotation) //no result
大小寫字串
可以從乙個string型別的uppercasestring 和 lowercasestring中獲得乙個字串的大寫或小寫。
let sentence = "asssffarqfasdasfasafd"
let shoutly = sentence.uppercasestring
let whispered = sentence.lowercasestring
println("shoutly is \(shoutly) and whispered is \(whispered)")
//shoutly is asssffarqfasdasfasafd and whispered is asssffarqfasdasfasafd
10. unicode
unicode是編碼和表示文字的國際標準。它幾乎可以顯示所有語言的所有字元的標準形態。還可以從類似於文字檔案或者網頁這樣的外部原始檔中讀取和修改他們的字元。
每乙個unicode字元都能被編碼為乙個或多個unicode scalar。乙個unicode scalar是乙個唯一的21位數(或者名稱),對應著乙個字元或者標識。例如 u+0061是乙個小寫的a (「a」), 或者u+1f425是乙個面向我們的黃色小雞
swift 學習筆記三
這一章主要學習swift的陣列與字典 1 陣列的概念和定義 var array 2,3,4,5 寫法1 print array var array2 array a b c 寫法2 print array2 array2 var array3 array 4,5,6 寫法3 print array3...
老貓swift學習筆記 三 陣列
var arr string allen jerry haley colin jay println size arr.count 5 往陣列末尾新增資料linda arr linda 往陣列末尾新增資料angel 往陣列末尾新增陣列 arr alan adm for item in arr pri...
swift學習筆記
1 值永遠不會被隱式轉換為其他型別。如果你需要把乙個值轉換成其他型別,請顯式轉換。let label the width is let width 94 let widthlabel label string width could not find an overload for that acc...