字串及字元

字元指的是依照編碼格式的一個位元組(簡單來說就是一個英文字母、數字或符號),而字串是有序的字元集合(簡單說就是一段文字),皆是以一對雙引號"前後包起來。

字串字面量

在程式碼中包含一段預先定義的字串值作為字串字面量(string literal)。字串字面量是由一對雙引號""包著的具有固定順序的文字字元集合,可以為常數和變數提供初始值。

// 將一個字串字面量指派給一個常數
let someString = "Some string literal value"

多行字串字面量

如果你的字串需要以多行顯示,可以使用前後成對的三個雙引號"""包起來,來表示一個多行字串:

// 顯示多行字串字面量
let someMultiLineString = """
多行文字
也就是多行字串字面量
可以一併顯示
"""

初始化空字串

將空的字串字面量指派給變數,或是也可以初始化一個新的String變數:

// 這兩個是一樣的意思
var emptyString = ""
var anotherEmptyString = String()

字串可變性

將一個特定的字串指派給一個變數,之後還可以對其修改。而字串指派給一個常數,則無法再做修改,例子如下:

var variableString = "Cat"
variableString = "Book"
// variableString 現在為 Book

let constantString = "Sun"
//constantString = "Moon" // 這行會報錯誤 因為常數不能被修改

使用字元

字串是有序的字元集合,所以可以使用for-in迴圈來遍歷字串中的每一個字元:

for character in "Dog!" {
    print(character)
}
// 依序印出
// D
// o
// g
// !

後面章節會正式介紹 for-in 的使用方法

連接字元及字串

可以簡單的使用加號+將兩個字串連結在一起,加號指派運算+=同樣也可以使用。字元也是一樣的使用方式。

let str = "Hello"
let secondStr = ", world ."
var anotherStr = str + secondStr
// 印出:Hello, world .
print(anotherStr)

anotherStr += " Have a nice day ."
// 印出:Hello, world . Have a nice day .
print(anotherStr)

字串插值

可以使用反斜線\接著小括號()\(變數、常數或表達式)來將其內的值插入到一個字串中。

let str1 = "Sunday"
var anotherStr1 = "It is \(str1) ."
// 印出:It is Sunday .
print(anotherStr1)

// 表達式也可以
// 印出:I have 13 cars .
print("I have \(1 + 2 * 6) cars .")

特殊符號

字串中可以使用下面這些特殊符號:

  • 跳脫字元:\0(空字元)、\\(反斜線)、\t(水平 tab)、\n(換行)、\r( Return )、\"(雙引號)、\'(單引號)。
  • Unicode 純量:寫成\u{n}(u為小寫),其中 n 為任意一到八位十六進制數且可用的 Unicode 位碼。
// 印出 "Imagination is more important than knowledge" - Einstein
let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"

let dollarSign = "\u{24}"        // $,  Unicode 純量 U+0024
let blackHeart = "\u{2665}"      // ♥,  Unicode 純量 U+2665

計算字串中的字元數量

let str2 = "What a lovely day !"

// 印出字元數量:19
print(str2.count)

比較字串

有三種方式來比較字串:

  • 字串相同或不同 ==!=
  • 前綴相同 hasPrefix
  • 後綴相同 hasSuffix
let str3 = "It is Sunday ."
let str4 = "It is Sunday ."
let str5 = "It is Saturday ."

// 兩個字串相同 所以成立
if str3 == str4 {
    print("Success")
}
// 印出:Success

// str4 有前綴字串 It is 所以成立
if str4.hasPrefix("It is") {
    print("Success")
}
// 印出:Success

// str5 沒有後綴字串 Sunday . 所以不成立
if str5.hasSuffix("Sunday .") {
    print("Success")
} else {
    print("Failure")
}
// 印出:Failure

可以看到有str.count或是str.hasPrefix()這種以小數點.連接的表示方式,代表的是這個變數的屬性或是方法。

使用方法會依照其設定的規則表示,像是str.count是字元集合的字元數量。而str.hasPrefix()則是會對變數作處理後再返回。往後會很常見到這種用法。

範例

本節範例程式碼放在 ch1/strings_characters.playground

results matching ""

    No results matching ""