var num1 = "hello,world" var name = "xiaoming" var age = 18 let student = num1 + name let info = name + String(age) print(name + "\(age)")
var string1 = " " if string1.isEmpty { print("empty") }else{ print("NO") } // 打印结果:empty
var string1 = "mingtianhenhao" var string2 = "mingtianhenhao" if string1 ==string2{ print("相等") }else{ print("不相等") } //打印结果:相等
① hasPrefix 是否以某一个字符开头 if string1.hasPrefix("m"){ .... }else{....} ② hasSuffix 是否以某一个字符结尾 if string1.hasSuffix("o"){.....}else{ .... }
var str = "hello,world" str.characters.count // 字符串的字符数(个数) for i in str.characters { print (i) }
var str = "hello,world"
let result = (str as NSString).substringWithRange(NSMakeRange(2,3)) //....打印结果 :llo
// 打印:2 表示从左向右 0.1.2-> 从2开始 包含2 let start = str.startIndex.advanceBy(2) // 打印从右向左 0.9.8.7-> 从0开始 包含-1 let end = str.endIndex.advanceBy (-1) let range = Range<string.index>(statr:start,end:end)
// 打印:llo,worl let loc = str.subStringWithRange(range) //打印:llo,world 包含2 let statrloc = str.subStringFormIndex(statr) //打印 hello,worl end的位置在字母d 那里. let endloc = str.subStringToIndex(end)