Swift字符串常用操作总结
版本2:增加了Swift 2.0的语法,与Swift 1.2的语法相比,主要是:advance方法变成了advancedBy方法(但不是简单替换);没有了count方法,count(str)需要变成str.characters.count等。
字符串的定义
varstr1="hello, mandarava."//字符串变量letstr2="hello, mandarava."//字符串常量letstr3=""//空字符串letstr4=String()//空字符串
字符的定义
varchar1:Character="m"//字符变量letchar2:Character="m"//字符常量
字符串的连接
letstr1="hello, "letstr2="mandarava."letstr3=str1+str2//=连接str1、str2//str3="hello, mandarava."//----------------------------------------letstr4="\(str1)\(str2)"//=连接str1、str2//str4="hello, mandarava."//----------------------------------------letstrArray=["apple","orange","cherry"]letjoinStr=",".join(strArray)//joinStr="apple,orange,cherry"
字符串与字符的连接
letchar1:Character="o"varstr1="hell"letstr2=str1+String(char1)//str2="hello"//----------------------------------------letstr3="\(str1)\(char1)"//str3="hello"//----------------------------------------str1.append(char1)//str1="hello"
字符串与其它类型值的连接
letxx=10letyy=10.8letstr1="\(xx)+\(yy)=?"//str1="10+10.8=?"//----------------------------------------letstr2=String(format:"%i+%.1f=?", xx, yy)//str2="10+10.8=?"
字符串枚举字符
//Swift 1.2letstr="mandarava"forchinstr{println(ch)}//-----------------------//Swift 2.0letstr="mandarava"forchinstr.characters{print(ch)}
获取字符串中指定索引处的字符
let str="Mandarava"letchr=str[advance(str.startIndex,2)] //Swift1.2//chr:Character="n"letchr=str[str.startIndex.advancedBy(2)] //Swift2.0//chr:Character="n"
计算字符串长度length
letstr="@鳗驼螺"letlen1=count(str)//swift 1.2 //=4letlen2=str.characters.count//swift 2.0 //=4letblen=str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)//=10
字符串比较
letstr1="hello,"letstr2="mandarava."letstr3="hello,mandarava."letcomp1 = str1==str2 //falseletcomp2 = str1+str2 == str3 //trueletcomp3 = str1 < str2 //trueletcomp4 = str1 != str2 //true
是否包含子串contains
letstr1="hello,mandarava."letstr2="mandarava"letrange=str1.rangeOfString(str2)ifrange !=nil{//包含}
查找子串indexof
varstr="hi,Mandarava."letrange=str.rangeOfString("Mandarava", options: NSStringCompareOptions.allZeros)//Swift 1.2letrange=str.rangeOfString("Mandarava", options: NSStringCompareOptions())//Swift 2.0letstartIndex=range?.startIndex//=3
首字母大写capitalized
varstr1="mandarava is a flower."str1.capitalizedString//str1="Mandarava Is A Flower.
转换为大写字母uppercase
varstr1="hello, mandarava."str1=str1.uppercaseString//str1="HELLO, MANDARAVA."
转换为小写字母lowercase
varstr1="HELLO, MANDARAVA."str1=str1.lowercaseString//str1="hello, mandarava."
截取字符串substring
letstr1="hello,mandarava."letstr2=str1.substringFromIndex(advance(str1.startIndex,6))//Swift 1.2letstr2=str1.substringFromIndex(str1.startIndex.advancedBy(6))//Swift 2.0//str2="mandarava."//----------------------------------------letstr3=str1.substringToIndex(advance(str1.startIndex,5))//Swift 1.2letstr3=str1.substringToIndex(str1.startIndex.advancedBy(5))//Swift 2.0//str3="hello"//----------------------------------------letrange=Range(start:advance(str1.startIndex,6), end:advance(str1.endIndex, -1))//Swift 1.2letrange=Range(start: str1.startIndex.advancedBy(6), end: str1.endIndex.advancedBy(-1))//Swift 2.0letstr4=str1.substringWithRange(range)//str4="mandarava"
字符串修剪trim
letstr1=" mandarava.\n "letstr2=str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())//str2="mandarava."//----------------------------------------letstr3=str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())//str3="mandarava.\n"//----------------------------------------letcharset=NSCharacterSet(charactersInString:" \n")letstr4=str1.stringByTrimmingCharactersInSet(charset)//str4="mandarava."
字符串的分解子串split
var str1="boy, girl, man, woman"letstr1Array=str1.componentsSeparatedByString(",")//str1Array=["boy"," girl"," man"," woman"]var str2="boy,girl,man 10 20 30"letcharset=NSCharacterSet(charactersInString:", ")letstr2Array=str2.componentsSeparatedByCharactersInSet(charset)//str2Array=["boy","girl","man","10","20","30"]
字符串替换replace
varstr1="My name is Mandarava."letsubRange=Range(start: str1.startIndex, end:advance(str1.startIndex,2))//Swift 1.2letsubRange=Range(start: str1.startIndex, end: str1.startIndex.advancedBy(2))//Swift 2.0str1.replaceRange(subRange, with:"Your")//str1="Your name is Mandarava."varstr2="hello, Mandarava."str2=str2.stringByReplacingOccurrencesOfString("Mandarava", withString:"鳗驼螺")//str2="hello, 鳗驼螺."str2=str2.stringByReplacingOccurrencesOfString("鳗驼螺", withString:"Mandarava", options:NSStringCompareOptions.CaseInsensitiveSearch, range:nil)//str2="hello, Mandarava."
string转换为Int/Long/Float/Double/Bool等
varstr1="100"vari=str1.toInt()!//Swift 1.2 //=100vari=(str1asNSString).integerValue//Swift 2.0 //=100vari=(str1asNSString).intValue//=100varl=(str1asNSString).longLongValue//=100varstr2="10.8"varf=(str2asNSString).floatValue//=10.8vard=(str2asNSString).doubleValue//=10.8varstr3="true"varb=(str3asNSString).boolValue//=true