博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift3.0 split函数切割字符串
阅读量:7244 次
发布时间:2019-06-29

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

我们先看函数的原型:

[objc]
  1. public func split(separator: Self.Iterator.Element, maxSplits: Int = default, omittingEmptySubsequences: Bool = default) -> [Self.SubSequence]  

第一个参数就不用解释了,传入要切割的字符串,像这样

[objc]
  1. let line = "BLANCHE:   I don't want realism. I want magic!"  
  2. print(line.characters.split(separator: " ")  
  3.                      .map(String.init))  
  4. // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"  

下面看第二个参数,像这样,意思是切割几次,设置为1的话就把原来的字符串切成两个。

 

[objc]
  1. // The second example passes `1` for the `maxSplits` parameter, so the  
  2. // original string is split just once, into two new strings.  
  3.   
  4. print(line.characters.split(separator: " ", maxSplits: 1)  
  5.                      .map(String.init))  
  6. // Prints "["BLANCHE:", "  I don\'t want realism. I want magic!"]"  

第三个参数就很明确了,是否保留隐藏字符

 

[objc]
  1. // The final example passes `false` for the `omittingEmptySubsequences`  
  2. // parameter, so the returned array contains empty strings where spaces  
  3. // were repeated.  
  4.   
  5. print(line.characters.split(separator: " ", omittingEmptySubsequences: false)  
  6.                       .map(String.init))  
  7. // Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"  

看看官方文档对这三个参数的解释,懒得翻译了,也不是很难懂。

 

[objc]
    1. /// - Parameters:  
    2. ///   - separator: The element that should be split upon.  
    3. ///   - maxSplits: The maximum number of times to split the collection, or  
    4. ///     one less than the number of subsequences to return. If  
    5. ///     `maxSplits + 1` subsequences are returned, the last one is a suffix  
    6. ///     of the original collection containing the remaining elements.  
    7. ///     `maxSplits` must be greater than or equal to zero. The default value  
    8. ///     is `Int.max`.  
    9. ///   - omittingEmptySubsequences: If `false`, an empty subsequence is  
    10. ///     returned in the result for each consecutive pair of `separator`  
    11. ///     elements in the collection and for each instance of `separator` at  
    12. ///     the start or end of the collection. If `true`, only nonempty  
    13. ///     subsequences are returned. The default value is `true`.  
    14. /// - Returns: An array of subsequences, split from this collection's  
    15. ///   elements. 

转载地址:http://yxybm.baihongyu.com/

你可能感兴趣的文章
Swift中Class和Struct异同
查看>>
Docker 共有 13 个管理命令和 41 个通用命令,以下是常用 Docker 命令列表
查看>>
HDU 1874 畅通工程续【Floyd算法实现】
查看>>
【经验分享】安装VirtualBox的时候遇到的问题
查看>>
Java-日历表
查看>>
GLPaint in OpenGL ES 2.0
查看>>
CocoaAsyncSocket学习
查看>>
关于form.item不兼容的问题
查看>>
poj Supermarket
查看>>
常用变量的获取
查看>>
洛谷9月月赛round2
查看>>
Lazy的SDL教程 翻译----Lesson 22 Timing
查看>>
Character.UnicodeBlock中cjk的说明
查看>>
Visual Studio 2013 滚动条实现代码缩略图
查看>>
C#调用带输出参数的mysql存储过程
查看>>
Linux之awk学习(二)
查看>>
恩,终于有点时间了,开篇
查看>>
ajax发送data的三种方式
查看>>
js的数据类型具体分析
查看>>
【转载】理解ASP.NET MVC中的ActionResult
查看>>