Swift字符串類型

字符串初始化html

 一、初始化ide

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片函數

  1. let  someString        =   "Some      string    literalvalue"  ui

  2.    

  3.    

  4.    

  5. let wiseWords = "\"Imagination is moreimportant than knowledge\" -Einstein" let  dollarSign      =   "\x24"                  //  $,  spa

  6. Unicodescalar U+0024  .net

  7.    

  8. let  blackHeart       =   "\u2665"               //  ♥,  scala

  9. Unicodescalar U+2665  code

  10.    

  11. let  sparklingHeart        =  "\U0001F496"           //  orm

  12.    

  13. , Unicode scalarU+1F496  htm


二、空值

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. var emptyString = ""                                //  

  2. 空串初始化  

  3. var anotherEmptyString = String()              //  通 過初始化函數初始化  

  4.    


 

三、空值判斷

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. if emptyString.isEmpty{  

  2.    

  3. println("Nothing to see here")  

  4.    

  5. }  


 

 

 

 

字符串修改

 

var 聲明的能夠修改,let 不能修改。

 

 

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. var variableString = "Horse" variableString+= " and carriage"  

  2.    

  3.    

  4.    

  5.    

  6. let constantString = "Highlander" constantString  += "  and   another  

  7.   

  8. Highlander" //錯誤  

  9.    


 

字符串插入

 

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. let multiplier = 3  

  2.    

  3. let message = "\(multiplier) times 2.5 is  

  4. \(Double(multiplier) * 2.5)"  


 

字符串長度

 

使用 countElements函數。

 

 

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. let  unusualMenagerie            =  "Koala     , Snail, Penguin,Dromedary" println("unusualMenagerie                     has  

  2. \(countElements(unusualMenagerie))  

  3. characters")  


比較字符串相等

 

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. let quotation = "We're a lot alike, you and I."let sameQuotation = "We're a lot alike, youand I."  

  2.    

  3.    

  4. if quotation == sameQuotation {  

  5.    

  6. println("These         two       strings      areconsidered equal")  

  7.    

  8. }  

 Swift交流討論論壇論壇:http://www.cocoagame.net
歡迎加入Swift技術交流羣:362298485

相關文章
相關標籤/搜索