我们先看函数的原型:

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


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

  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的话就把原来的字符串切成两个。

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

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

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

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

    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.

Swift3.0 split函数切割字符串的更多相关文章

  1. Swift3.0语言教程分割字符串与截取字符串

    Swift3.0语言教程分割字符串与截取字符串 Swift3.0语言教程分割字符串 如果想要快速的创建一个数组,我们可以将字符串进行分割,分割后的内容将会生成一个数组.在NSString中有两个分割字 ...

  2. Swift3.0语言教程组合字符串

    Swift3.0语言教程组合字符串 Swift3.0语言教程组合字符串,当开发者想要将已经存在的字符串进行组合,形成一个新的字符串,可以使用NSString中的两个方法,分别为appending(_: ...

  3. Swift3.0语言教程获取字符串编码与哈希地址

    Swift3.0语言教程获取字符串编码与哈希地址 Swift3.0语言教程获取字符串编码与哈希地址,以下将讲解字符串中其它内容的获取方法. 1.获取字符串编码 在NSString中可以使用2个属性获取 ...

  4. Swift3.0语言教程获取字符串长度

    Swift3.0语言教程获取字符串长度 Swift3.0语言教程获取字符串长度,当在一个字符串中存在很多的字符时,如果想要计算字符串的长度时相当麻烦的一件事情,在NSString中可以使用length ...

  5. Swift3.0语言教程使用字符串创建和初始化字符串

    Swift3.0语言教程使用字符串创建和初始化字符串 Swift3.0语言教程使用字符串创建和初始化字符串,在编程语言中,字面值是很常见的数据描述形式.人们可以通过字面所表达的意思,获知其含义,尤其是 ...

  6. PHP 中使用explode()函数切割字符串为数组

    explode()函数的作用:使用一个字符串分割另一个字符串,打散为数组. 例如: 字符串 $pizza = "第1 第2 第3 第4 第5 第6"; 根据空格分割后:$piece ...

  7. javascript 中 split 函数分割字符串成数组

    分割字符串成数组的方法有很多,不过使用最多的还是split函数 <script language="javascript"> str="2,2,3,5,6,6 ...

  8. split+ Pattern切割字符串

    今天在对一个String对象进行拆分的时候,总是无法到达预计的结果.呈现数据的时候出现异常,后来debug之后才发现,错误出在String spilt上,于是开始好好研究下这东西,开始对api里的sp ...

  9. Java开发:字符串切割split函数——切割符转码注意事项

    一.问题如下: 1.先对一个已有字符串进行操作,使用     ;     进行分割: //示例字符串 String string="sr1.db1.tb1.df1;sr2.db2.tb2.d ...

随机推荐

  1. RPC与REST

    RPC与REST (摘自网络,个人理解)

  2. http接口 两种调用http接口的方法

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; ...

  3. 浅谈Websocket、Ajax轮询和长轮询(long polling)

    浅谈Websocket.Ajax轮询和长轮询(long p0ll) 最近看到了一些介绍Websocket的文章,觉得挺有用,所以在这里将自己的对其三者的理解记录一下. 1.什么是Websocket W ...

  4. win7 64位装sql2000

    1.运行不了安装程序 右击安装exe文件->属性->兼容性->以xp sp3兼容和管理员身份 2.安装过程中提示“被挂起”的故障 解决:打开注册表编辑器,在HKEY_LOCAL_MA ...

  5. ASP.NET刷新页面的六种方法

    第一: private void Button1_Click( object sender, System.EventArgs e )   {      Response.Redirect( Requ ...

  6. 时序分析:DTW算法(基于模板)

    对时序对象进行分析,使用KMP算法可以分析速率不变的模式,参考时序分析:欧式空间轨迹模式识别.使用基于模板匹配的方法,对于速率发生变化的模式,需要用新的对速率要求松散的方法,DTW方法为一种广泛使用的 ...

  7. sqlserver where in 在 mysql

    ) tmp); 主句(select * from (从句 temp) sql的 where in 删除  要更改为  // in( select * from ((select idfrom twhe ...

  8. windows 下安装mysql 成功版

    mysql 下载地址 http://dev.mysql.com/downloads/ zip版下载 解压到本地 假设文件保存在C:\mysql-5.7.17-winx64 1.以管理员身份运行cmd. ...

  9. Python学习笔记之类与对象

    这篇文章介绍有关 Python 类中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中类的使用技巧 1.与类和对象相关的内置方法 issubclass(class, classinfo) ...

  10. C#学习笔记_03_运算符

    03_运算符 算数运算符 + - * / % ++ -- 赋值运算符 = += -= *= /= %= 关系运算符 > < >= <= == != 逻辑运算符 &:逻辑 ...