博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift字符串常用操作总结
阅读量:5073 次
发布时间:2019-06-12

本文共 5238 字,大约阅读时间需要 17 分钟。

版本2:增加了Swift 2.0的语法,与Swift 1.2的语法相比,主要是:advance方法变成了advancedBy方法(但不是简单替换);没有了count方法,count(str)需要变成str.characters.count等。

  1. 字符串的定义
    var str1="hello, mandarava." //字符串变量let str2="hello, mandarava." //字符串常量 let str3="" //空字符串 let str4=String() //空字符串
  2. 字符的定义
    var char1:Character="m" //字符变量let char2:Character="m" //字符常量
  3. 字符串的连接
    let str1="hello, "let str2="mandarava."let str3=str1+str2 //=连接str1、str2 //str3="hello, mandarava." //---------------------------------------- let str4="\(str1)\(str2)" //=连接str1、str2 //str4="hello, mandarava." //---------------------------------------- let strArray=["apple", "orange", "cherry"] let joinStr=",".join(strArray) //joinStr="apple,orange,cherry"
  4. 字符串与字符的连接
    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"
  5. 字符串与其它类型值的连接
    let xx=10let yy=10.8let str1="\(xx)+\(yy)=?" //str1="10+10.8=?" //---------------------------------------- let str2=String(format: "%i+%.1f=?", xx, yy) //str2="10+10.8=?"
  6. 字符串枚举字符
    //Swift 1.2let str="mandarava"for ch in str{ println(ch) } //----------------------- //Swift 2.0 let str="mandarava" for ch in str.characters{ print(ch) }
  7. 获取字符串中指定索引处的字符
    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"
  8. 计算字符串长度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
  9. 字符串比较
    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
  10. 是否包含子串contains
    let str1="hello,mandarava."let str2="mandarava"let range=str1.rangeOfString(str2) if range != nil{ //包含 }
  11. 查找子串indexof
    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
  12. 首字母大写capitalized
    var str1="mandarava is a flower."str1.capitalizedString//str1="Mandarava Is A Flower.
  13. 转换为大写字母uppercase
    var str1="hello, mandarava."str1=str1.uppercaseString//str1="HELLO, MANDARAVA."
  14. 转换为小写字母lowercase
    var str1="HELLO, MANDARAVA."str1=str1.lowercaseString//str1="hello, mandarava."
  15. 截取字符串substring
    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
    (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="mandarava"
  16. 字符串修剪trim
    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."
  17. 字符串的分解子串split
    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"]
  18. 字符串替换replace
    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."
  19. 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 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

文/鳗驼螺(简书作者)
原文链接:http://www.jianshu.com/p/52e7580166ff
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

转载于:https://www.cnblogs.com/Free-Thinker/p/6088171.html

你可能感兴趣的文章
取数据库MDF文件存储路径SQL语句
查看>>
【转】C#使用PrintDocument打印 多页 打印预览
查看>>
PHP list() 函数
查看>>
(转载)重新对APK文件签名
查看>>
layui弹窗、字符串转一维数组
查看>>
Qt打包成单独可执行的exe文件
查看>>
test
查看>>
1.7 HelloWorld 添加视图
查看>>
设计模式-写在前面
查看>>
bat脚本学习
查看>>
asp.net应用程序脱机app_offline.htm文件
查看>>
XMind 入门教程
查看>>
ElasticSearch restful实操
查看>>
Python实现图片转文字并翻译至剪切板
查看>>
条件语句中出现多个OR的情况
查看>>
java List接口实现类
查看>>
遗忘的基础概念——并发、并行、进程、线程
查看>>
洛谷1094 纪念品分组
查看>>
php:自己设计的一个php验证码
查看>>
微信扫码支付
查看>>