003 Swift3 0之字串型別

2022-07-15 04:36:09 字數 3673 閱讀 4502

一、空字串

var emptystring1 = ""

var emptystring2 =string()

//判斷是否為空

ifemptystring1.isempty

二、字串可變性

1.常量字串(let宣告的字串)完成賦值之後不可變

2.變數字串(var宣告的字串)可變

var varstring = "

hello

"varstring += "

, frank

"let letstring = "

highlander"//

letstring += "frank" (報錯,不可變)

三、字串的值型別

在swift中,字串的賦值和傳值都是進行重新拷貝乙份,因此它是值型別,僅僅只是拷貝值而已,是獨立存在的

四、字串&字元

1.屬性:characters

var string1 = "

hello, frank

"for character in

string1.characters

2.字元的宣告

注意:字元的宣告必須顯式宣告,否則系統會誤認為是string型別

let c1: character = "!"

var c2: character = "

h"

3.字元轉化為字串

①通過字串初始化函式

let character: character = "s"

let characterarray: [character] = ["

f", "

r", "

a", "

n", "

k", "

c", "

o", "

m", "

e", "

o", "

n", "!"

]let str1 = string(character); //

字元轉字串

let str2 = string(characterarray) //

字符集轉字串

②通過連線運算子

let s1 = "

hello

"let s2 = "

frank

"var s3 = s1 +s2

let s4 = "

welcome

"var s5 = "

to huanggang

"s5 += s4

let cc: character = "!"

let ss = "hg"

var ss1 = "

welcome to huanggang

"

④任意基本資料型別通過\()轉化為字串

let ccc: character = "m"

let nnn = 27

let mmm = 169.10

let sss = "

frank

"let s = "

\(ccc)y name is \(sss), i'm \(nnn) years old! my height is \(mmm) cm.

"

五、字串的字元數量

let sss1 = "

koala

swift3 0 關於字串

playground noun a place where people can play import uikit 字串字面量 func zfczml unicode 標量 u 0024 u unicode 標量 u 2665 u unicode 標量 u 1f496 初始化空字串 func cs...

swift 過濾字串 Swift 字串

swift 字串 swift 字串是一系列字元的集合。例如 hello,world 這樣的有序的字元型別的值的集合,它的資料型別為 string。建立字串 你可以通過使用字串字面量或 string 類的例項來建立乙個字串 import cocoa 使用字串字面量 var stringa hello,...

swift3 0 String 字串 的使用

var emptystring 空字串字面量 var anotheremptystring string 初始化方法 兩個字串均為空並等價。var variablestring horse variablestring and carriage variablestring 現在為 horse an...