Swift 字元於字串

來源:互聯網
上載者:User

標籤:style   blog   color   io   for   re   c   cti   

Swift 的 String 和 Character 類型??供了一個快速的,相容 Unicode 的方式來處理代碼 

字串常量

let strTest = "I‘m Swift"

字串空值判斷

let  emptyString = ""if emptyString.isEmpty {2. println("Nothing to see here")3. }

字串可變性。字串的可變性是通過var還是let來確定的。var表示是可變字串變數,而let表示是字串常量。

var strMsg = "test message"strMsg = "reset message"let strConst = "msg const"

字串拼接 String支援 + 和 += 操作

var strMsg = "msg "let strConst = "const "strMsg = strMsg + strConststrMsg += "endl"println("\(strMsg)")

 字串String是單個unicode字元Character的集合

for char in "tst** 韓" {    println("\(char)\n")  }

String 字元個數。用countElements函數

var strTest = "c插是te"println("\(countElements(strTest))")

String比較提供了三種方式來比較字串的值:字串相等,首碼相等和尾碼相等。 String用  == 做比較 

hasPrefix/hasSuffix 方法來檢查字串是否擁有特定首碼/尾碼 

var strTest = "c插是te"var strSecond = "c插是te"if strTest == strSecond {      println("same");}if strTest.hasSuffix("te") {    println("has suf");}

String 大小寫版本 

通過字串的 uppercaseString 和 lowercaseString  返回

 

let normal = "Hello, Ketty!你好"var shouty = normal.uppercaseStringvar whispered = normal.lowercaseStringprintln("\(shouty)\n\(whispered)")//輸出HELLO, KETTY!你好hello, ketty!你好

 

Character型別宣告

let chTest  : Character = "插" //這裡是雙引號。

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.