Swift字符串类型
版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/tonny_guan/article/details/31407137
字符串初始化
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交流讨论论坛论坛:http://www.cocoagame.net欢迎增加Swift技术交流群:362298485