Swfit3.0關(guān)于String串操作最搞是的坐標(biāo)索引變了,不能用原來的int直接定位
看一下,前后變化,以一個截取rgb字符串代碼為例:
swift2.3的寫法:
----------------------------------------------
letredStr = (StrasNSString).substringToIndex(2)
letgreenStr = ((StrasNSString).substringFromIndex(2)asNSString).substringToIndex(2)
letblueStr = ((StrasNSString).substringFromIndex(4)asNSString).substringToIndex(2)
而swift 3.0的寫法:
---------------------------------------------------
letredStr =
Str[Str.startIndex...Str.index(Str.startIndex,offsetBy:1)]
letgreenStr =
Str[Str.index(Str.startIndex, offsetBy:2)...Str.index(Str.startIndex, offsetBy:3)]
letblueStr =
Str[Str.index(Str.startIndex, offsetBy:4)...Str.index(Str.startIndex, offsetBy:5)]
-----------------------------------------------------
索引全部要用
串名.index(串名.startindex,offsetBy:偏離數(shù))
串名.index(串名.endindex,offsetBy:偏離數(shù))
如果比較長可以先用let fromindex:String.index=Str.index(Str.startIndex, offsetBy:3) 的試再解析出索引值 ,再放到截取方法中 ,這樣可讀性更強