字符串索引 (String Indices)
每一个String值都有一个关联的索引(index)类型,String.Index,它对应着字符串中的每一个Character的位置。
不同的字符可能会占用不同数量的内存空间,所以要知道Character的确定位置,就必须从String开头遍历每一个 Unicode 标量直到结尾。因此,Swift 的字符串不能用整数(integer)做索引。
使用startIndex属性可以获取一个String的第一个Character的索引。使用endIndex属性可以获取最后一个Character的后一个位置的索引。因此,endIndex属性不能作为一个字符串的有效下标。如果String是空串,startIndex和endIndex是相等的。
通过调用String.Index的predecessor()方法,可以立即得到前面一个索引,调用successor()方法可以立即得到后面一个索引。任何一个String的索引都可以通过锁链作用的这些方法来获取另一个索引,也可以调用advancedBy(_:)方法来获取。但如果尝试获取出界的字符串索引,就会抛出一个运行时错误。
1.获取字符串中指定索引处的字符
let str = "sunke"
//Swift 1.2 //chr:Character="n"
let chr = advance(str.startIndex,2)
//Swift 2.0 //chr:Character="n"
let chr = str.startIndex.advancedBy(2)
2.计算字符串长度length
let str = "@小码哥"
let len1 = count(str) //swift 1.2 //=4
let len2 = str.characters.count //swift 2.0 //=4
let blen = str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) //=10
3.字符串比较
let str1 = "hello,"
let str2 = "sk."
let str3 = "hello,sk."
let comp1 = str1==str2 //false
let comp2 = str1+str2 == str3 //true
let comp3 = str1 < str2 //true
let comp4 = str1 != str2 //true
4.是否包含子字符串contains
let str1 = "hello,sk."
let str2 = "sk"
let range = str1.rangeOfString(str2)
if range != nil{
print("包含")
}
5.查找子字符串indexof
var str = "hi,sunke."
let range = str.rangeOfString("sunke") //Swift 2.0
let startIndex = range?.startIndex //=3
6.首字母大写capitalized
var str1 = "sunke is a coder."
str1.capitalizedString
//str1 = "Sunke Is A Coder."
7.转换为大写字母uppercase
var str1 = "hello, sunke."
str1 = str1.uppercaseString
//str1 = "HELLO, SUNKE."
8.转换为小写字母lowercase
var str1 = "HELLO, SUNKE."
str1 = str1.lowercaseString
//str1 = "hello, sunke."
9.截取字符串substring
let str1 = "hello,sunke."
let str2 = str1.substringFromIndex(advance(str1.startIndex, 6)) //Swift 1.2
let str2 = str1.substringFromIndex(str1.startIndex.advancedBy(6)) //Swift 2.0
//str2 = "sunke."
//----------------------------------------
let str3 = str1.substringToIndex(advance(str1.startIndex, 5)) //Swift 1.2
let str3 = str1.substringToIndex(str1.startIndex.advancedBy(5)) //Swift 2.0
//str3 = "hello"
//----------------------------------------
let range = Range(start: advance(str1.startIndex, 6), end: advance(str1.endIndex, -1)) //Swift 1.2
let range = Range(start: str1.startIndex.advancedBy(6), end: str1.endIndex.advancedBy(-1)) //Swift 2.0
let str4 = str1.substringWithRange(range)
//str4 = "sunke"
10.字符串首尾修剪
//去掉首尾空格和换行符
let str1 = " sunke.\n "
let str2 = str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
//str2 = "sunke."
//----------------------------------------
//去掉首尾空格
let str3 = str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
//str3 = "sunke.\n"
//----------------------------------------
//去掉首尾空格和\n
let charset = NSCharacterSet(charactersInString:" \n")
let str4 = str1.stringByTrimmingCharactersInSet(charset)
//str4="sunke."
11.字符串和数组转换
var str1 = "boy, girl, man, woman"
let str1Array = str1.componentsSeparatedByString(",")
//str1Array = ["boy", " girl", " man", " woman"]
var str2 = "boy,girl,man 10 20 30"
let charset = NSCharacterSet(charactersInString:", ")
let str2Array = str2.componentsSeparatedByCharactersInSet(charset)
//str2Array = ["boy", "girl", "man", "10", "20", "30"]
12.字符串替换replace
var str1 = "My name is Sunke."
let subRange = Range(start: str1.startIndex, end: advance(str1.startIndex, 2)) //Swift 1.2
let subRange = Range(start: str1.startIndex, end: str1.startIndex.advancedBy(2)) //Swift 2.0
str1.replaceRange(subRange, with: "Your")
//str1 = "Your name is Sunke."
var str2 = "hello, Sunke."
str2 = str2.stringByReplacingOccurrencesOfString("Sunke", withString: "iOS")
//str2 = "hello, iOS."
//CaseInsensitiveSearch :不区分大小写的搜索
str2 = str2.stringByReplacingOccurrencesOfString("iOS", withString: "Sunke", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
//str2="hello, Sunke."
13.string转换为Int/Long/Float/Double/Bool等
var str1 = "100"
var i = str1.toInt()! //Swift 1.2 //=100
var i = (str1 as NSString).integerValue //Swift 2.0 //=100
var a = (str1 as NSString).intValue //=100
var l = (str1 as NSString).longLongValue //=100
var str2 = "10.8"
var f = (str2 as NSString).floatValue //=10.8
var d = (str2 as NSString).doubleValue //=10.8
var str3 = "true"
var b = (str3 as NSString).boolValue //=true