String 是有序的字符集合,例如“helloword”
Swift字符串採用String類型來表示,同時它也可以被Character類型的集合所表示。
創建空字符串
<span style="font-size:14px;">var emptyString = ""
var anotherEmptyString = String()</span>
判斷 字符串 是否爲空 通過檢查 isEmpty 屬性 返回的 Bool 值來實現
if emptyString.isEmpty{
print("emptyString is empty")
}
//emptyString is empty
可變性
字符串的可變與不可變 取決於 聲明的是變量 還是 常量。
var variableString = "Hello"
variableString += "word"
print("variableString is \(variableString)")
variableString is Helloword
let constantString = "Highlander"
constantString += " and another Highlander" //這裏會出現編譯錯誤
字符串的長度
字符串的長度可以通過以下方式獲取
let msg = "helloWord"
var count = msg.characters.count;
print("msg count is \(count)")
//msg count is 9
字符串的拼接
1,使用加法運算符(+)進行拼接,並創建一個新的字符串
let string1 = "hello"
let string2 = "word"
let string3 = string1 + string2
print("string3 is \(string3)")
let char1 :Character = "A"
let char2 : Character = "B"
let char3 = String (char1) + String(char2)
print("char3 is \(char3)")
print("string1+char1 is \(string1 + String(char1))")
//string3 is helloword
//char3 is AB
//string1+char1 is helloA
在很多教程上看到 String 類型 和 Character 類型,以及兩個 Character 類型的變量是直接可以進行 加法運算的,但是我在實際的代碼中並沒有成功,
所以在進行加法運算的時候,我把所有的Character 類型轉換成了String 類型
2,通過加法賦值運算符(+=)將一個字符串或者字符追加到一個已經存在的字符串變量後面:
var string1 = "hello"
var string2 = "swift"
string1 += string2
print("string1 is \(string1)")
var char1:Character = "!"
var welcome = "good morning"
welcome += String(char1)
print("welcome is \(welcome)")
//string1 is helloswift
//welcome is good morning!
注意:不能將一個字符串 或者 字符 追加到一個以存在的 字符變量後面,因爲一個字符值只能包含一個字符
字符串的插值
字符串插值是一種全新的構建字符串的方式,可以在其中包含常量、變量、字面量和表達式。你插入字符串字面量的每一項需要包裹在以反斜線爲前綴的圓括號中:
//插入變量
var welcome = "welcome to china"
print("you say \(welcome)")
//you say welcome to china
//插入常量
let morning = "good morning"
print("you say \(morning)")
//you say good morning
//插入表達式
let num:Double = 3.1415926
print("\(num) * 125 = \(num * 125) ")
//3.1415926 * 125 = 392.699075
字符串比較
Swift 提供了三種方式來比較字符串的值:字符串相等,前綴相等和後綴相等
字符串相等 使用 == 來比較
var str1 = "this is a dog"
var str2 = "this is a dog"
if str1 == str2{
print("str1 == str2")
}else{
print("str1 != str2")
}
//str1 == str2
判斷前綴
通過調用 hasPrefix 函數檢查給定的參數字符串是否以 string 爲前綴
var str1 = "this is a dog"
var str2 = "that is a cat"
if str1.hasPrefix("this"){
print("str1 hasPrefix this")
}else{
print("str1 not hasPrefix this")
}
if str2.hasPrefix("this"){
print("str2 hasPrefix this")
}else{
print("str2 not hasPrefix this")
}
//str1 hasPrefix this
//str2 not hasPrefix this
判斷後綴
通過調用hasSuffix 函數檢查給定的參數字符串是否以
string 爲後綴
<span style="font-size:14px;">var str1 = "this is a dog"
var str2 = "that is a cat"
if str1.hasSuffix("dog"){
print("str1 hasSuffix dog")
}else{
print("str1 not hasSuffix dog")
}
if str2.hasSuffix("dog"){
print("str2 hasSuffix dog")
}else{
print("str2 not hasSuffix dog")
}
//str1 hasSuffix dog
//str2 not hasSuffix dog</span>
字符串的大小寫
通過字符串的 uppercaseString 和 lowercaseString 屬性來訪問一個字符串的大寫或小寫的版本。
<span style="font-size:14px;">let normal = "Could you help me, please?"
let shouty = normal.uppercaseString
print("shouty is \(shouty)")
//shouty is COULD YOU HELP ME, PLEASE?
let whispered = normal.lowercaseString
print("whispered is \(whispered)")
//whispered is could you help me, please</span>