字符串初始化
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