版本2:增長了Swift 2.0的語法,與Swift 1.2的語法相比,主要是:advance方法變成了advancedBy方法(但不是簡單替換);沒有了count方法,count(str)須要變成str.characters.count等。javascript
var str1="hello, mandarava." //字符串變量 let str2="hello, mandarava." //字符串常量 let str3="" //空字符串 let str4=String() //空字符串
var char1:Character="m" //字符變量 let char2:Character="m" //字符常量
let str1="hello, " let str2="mandarava." let str3=str1+str2 //=鏈接str一、str2 //str3="hello, mandarava." //---------------------------------------- let str4="\(str1)\(str2)" //=鏈接str一、str2 //str4="hello, mandarava." //---------------------------------------- let strArray=["apple", "orange", "cherry"] let joinStr=",".join(strArray) //joinStr="apple,orange,cherry"
let char1:Character="o" var str1="hell" let str2=str1+String(char1) //str2="hello" //---------------------------------------- let str3="\(str1)\(char1)" //str3="hello" //---------------------------------------- str1.append(char1) //str1="hello"
let xx=10 let yy=10.8 let str1="\(xx)+\(yy)=?" //str1="10+10.8=?" //---------------------------------------- let str2=String(format: "%i+%.1f=?", xx, yy) //str2="10+10.8=?"
//Swift 1.2 let str="mandarava" for ch in str{ println(ch) } //----------------------- //Swift 2.0 let str="mandarava" for ch in str.characters{ print(ch) }
let str="Mandarava" let chr=str[advance(str.startIndex,2)] //Swift 1.2 //chr:Character="n" let chr=str[str.startIndex.advancedBy(2)] //Swift 2.0 //chr:Character="n"
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
let str1="hello," let str2="mandarava." let str3="hello,mandarava." let comp1 = str1==str2 //false let comp2 = str1+str2 == str3 //true let comp3 = str1 < str2 //true let comp4 = str1 != str2 //true
let str1="hello,mandarava." let str2="mandarava" let range=str1.rangeOfString(str2) if range != nil{ //包含 }
var str="hi,Mandarava." let range=str.rangeOfString("Mandarava", options: NSStringCompareOptions.allZeros) //Swift 1.2 let range=str.rangeOfString("Mandarava", options: NSStringCompareOptions()) //Swift 2.0 let startIndex=range?.startIndex //=3
var str1="mandarava is a flower." str1.capitalizedString //str1="Mandarava Is A Flower.
var str1="hello, mandarava." str1=str1.uppercaseString //str1="HELLO, MANDARAVA."
var str1="HELLO, MANDARAVA." str1=str1.lowercaseString //str1="hello, mandarava."
let str1="hello,mandarava." let str2=str1.substringFromIndex(advance(str1.startIndex, 6)) //Swift 1.2 let str2=str1.substringFromIndex(str1.startIndex.advancedBy(6)) //Swift 2.0 //str2="mandarava." //---------------------------------------- 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<String.Index>(start: advance(str1.startIndex, 6), end: advance(str1.endIndex, -1)) //Swift 1.2 let range=Range<String.Index>(start: str1.startIndex.advancedBy(6), end: str1.endIndex.advancedBy(-1)) //Swift 2.0 let str4=str1.substringWithRange(range) //str4="mandarava"
let str1=" mandarava.\n " let str2=str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) //str2="mandarava." //---------------------------------------- let str3=str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) //str3="mandarava.\n" //---------------------------------------- let charset=NSCharacterSet(charactersInString:" \n") let str4=str1.stringByTrimmingCharactersInSet(charset) //str4="mandarava."
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"]
var str1="My name is Mandarava." 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 Mandarava." var str2="hello, Mandarava." str2=str2.stringByReplacingOccurrencesOfString("Mandarava", withString: "鰻駝螺") //str2="hello, 鰻駝螺." str2=str2.stringByReplacingOccurrencesOfString("鰻駝螺", withString: "Mandarava", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil) //str2="hello, Mandarava."
var str1="100" var i=str1.toInt()! //Swift 1.2 //=100 var i=(str1 as NSString).integerValue //Swift 2.0 //=100 var i=(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