1。Swift字符串创建//类型推断letstr="Hello"//指定类型letstr1:String="Hello"2.Swift字符串拼接letstr1:String="Hello"letstr2="Hello"print(str1+"\n"+str2)//result:/**HelloHello*/3.Swiftstringappendvarstr="Hello"str.append("World")print(str)//结果:HelloWorld4。Swift字符串格式letstr=String(3)letstr1=String(5.0)print(str,str1)/**Result:35.0*///Formatletstr=String(format:"%dcourse",1)letstr1=String(format:"price%f",5.68)print(str,str1)//result:第一道菜的价格5.680000letstr2=String(format:"price%.2f",5.68)print(str2)//result:价格5.685。Swift获取字符串长度letstr=String(format:"price%.2f",5.68)//获取长度print(str.count)//result:66.Swift判断字符串是否为空letstr="swift"letstr1=""print(str.isEmpty)//result:falseprint(str1.isEmpty)//result:true7.Swift遍历字符串letstr="5.68"forcharinstr{print(char)//result:/**5.68*/}8.Swift字符串操作8.1获取第一个字符letstr="this"print(str[str.startIndex])//result:t8.2删除第一个字符Charactervarstr="ABC"str.removeFirst()//等价于str.remove(at:str.startIndex)print(str)//结果:BC8.3删除指定位置varstr="ABCDEFGH"str.remove(at:str.index(str.startIndex,offsetBy:2))print(str)//结果:ABDEFGH8.4删除最后一个字符varstr="ABC"str.removeLast()//等同于str.remove(at:str.index(str.endIndex,offsetBy:-1))print(str)//结果:AB8.5删除所有内容varstr="ABCDEFGH"str.removeAll()8.6删除varstr头尾指定数量的内容="ABCDEFGH"str.removeFirst(2)str.removeLast(2)print(str)//结果:CDEF9。Swift判断字符串是否相等letstr1:String="Hello"letstr="Hello"letstr2="Helloworld"//需要知道大小letisSame=str1.compare(str2)//枚举ComparisonResult-101print(isSame.rawValue)//result:-1print(str1.compare(str).rawValue)//result:0//只需要知道内容是否相等print(str1==str)//result:true10.Swift判断该字符串中包含另一个字符串letstr="Hello"letstr1="Helloworld"letrec=str1.contains(str)print(rec)//result:true11.Swiftstringsplitletstr="Helloworld"letarr=str.split(separator:"")print(arr)//结果:[“你好”,“世界”]12。Swift数组拼接字符串letarr=["Hello","World"]letjoined=arr.joined()print(joined)//结果:HelloWorldletsepJoined=arr.joined(separator:"----")print(sepJoined)//结果:你好----World13。Swift字符串截取//头部截取letstr="asdfghjkl;'"letstr1=str.prefix(2);print(str1)//结果:as//尾部截取letstr2=str.suffix(3);print(str2)//result:l;'//范围截取letindex3=str.index(str.startIndex,offsetBy:3)letindex4=str.index(str.startIndex,offsetBy:5)letstr5=str[index3...index4]print(str5)//result:fgh//得到指定位置字符串letrange=str.range(of:"jk")!print(str[str.startIndex..
