Swift 字符串和字符

swift中的字符串和字符都兼容unicode

定义一个字符串

let  hello="hello world"

初始化一个空的字符串

let hello=""

let hello=String()

判断一个字符串是否为空

if hello.isEmpty{

println("这是个空字符串")

}

字符串可以使用+进行连接

字符串是由单个字符组成

for 单个字符 in hello{

println(单个字符)

}

单独定义一个字符

var 单个字符:Character="u" 

在字符串中可以使用\() 在字符串中插入字面量 常量 变量

在swift用使用unicode可以这样写

let sparklingHear="\u{9999}" //汉字中的 “香”

计算字符串长度的方法 

可以使用全局函数countElements()

swift字符串的unicode展现

let str="hello world"

utf-8:str.utf8   utf-16:str.utf16   utf-32:str.unicodeScalars

 

posted @ 2014-11-16 11:53  jiashu.ge  阅读(298)  评论(0编辑  收藏  举报