码迷,mamicode.com
首页 > 其他好文 > 详细

Swift字符串类型

时间:2014-06-18 12:30:29      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:swift   swift教程   swift视频   cocos2d-x   

字符串初始化

 1、初始化

 

let  someString        =   "Some      string    literalvalue"
 
 
 
let wiseWords = "\"Imagination is moreimportant than knowledge\" -Einstein" let  dollarSign      =   "\x24"                  //  $,
Unicodescalar U+0024
 
let  blackHeart       =   "\u2665"               //  ?,
Unicodescalar U+2665
 
let  sparklingHeart        =  "\U0001F496"           //
 
, Unicode scalarU+1F496

2、空值

 

var emptyString = ""                                //
空串初始化
var anotherEmptyString = String()              //  通 过初始化函数初始化
 

 

3、空值判断

 

if emptyString.isEmpty{
 
println("Nothing to see here")
 
}

 

 

 

 

字符串修改

 

var 声明的可以修改,let 不能修改。

 

 

 

var variableString = "Horse" variableString+= " and carriage"
 
 
 
 
let constantString = "Highlander" constantString  += "  and   another

Highlander" //错误
 

 

字符串插入

 

 

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

 

字符串长度

 

使用 countElements函数。

 

 

 

let  unusualMenagerie            =  "Koala     , Snail, Penguin,Dromedary" println("unusualMenagerie                     has
\(countElements(unusualMenagerie))
characters")

比较字符串相等

 

 

let quotation = "We‘re a lot alike, you and I."let sameQuotation = "We‘re a lot alike, youand I."
 
 
if quotation == sameQuotation {
 
println("These         two       strings      areconsidered equal")
 
}
 Swift交流讨论论坛论坛:bubuko.com,布布扣http://www.cocoagame.net
欢迎加入Swift技术交流群:362298485

Swift字符串类型,布布扣,bubuko.com

Swift字符串类型

标签:swift   swift教程   swift视频   cocos2d-x   

原文地址:http://blog.csdn.net/tonny_guan/article/details/31407137

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!