字符串操作

01 获取长度

var a = "he l lo"
print(a.count) // 计算空格,输出7

02 String.Index类型

String.Index类型表示字符串内某一个字符的位置。

可以利用a[String.Index]来获取某一个位置的字符。

var a = "hello"
print(a.startIndex) // 输出Index(_rawBits: 1)
print(a[a.startIndex]) // 获取字符串第一位,输出h
  • 如何输出最后一位呢?

    print(a[a.endIndex]) // 报错,因为endIndex的位置是字符串的结束符,并不是看到的最后一个字符

    可以用a.index(before: String.Index)a.index(after: String.Index)分别获取某一个位置的前一位和后一位。

    a[a.index(before: a.endIndex)]就可以获取到字符串最后一个字符。

  • 如何输出指定的某一位?

    a[a.index(a.startIndex, offsetBy: 2)],意为:从a.startIndex开始,往后数两位。

    如果offsetBy后面使用了一个负数,那么就是从后往前数。

  • 输出某一段的字符

    var begin = a.index(a.startIndex, offsetBy: 1)
    var end = a.index(a.startIndex, offsetBy:4)
    print(str[begin...end]) // 输出ello

    或者使用prefix(Int)方法来获取前n个字符:

    var str = a.prefix(2)
    print(str) // 输出he
  • 如何找到第一次出现某字符的位置

    a.firstIndex(of: "e")

03 增删改查

1. 查

  • 判断字符是否在字符串中

    使用contains(Char)方法:(大小写敏感)

    var str = "hello"
    print(str.contains("h")) // true
    print(str.contains("hel")) // true

    也可以使用contains(where: String.contains(""))方法:(这种方法只要有一个包含就返回真值)

    var str = "hello"
    print(str.contains(where: String.contains("ae"))) // true
  • 判断字符串的开头或结尾是否是某字符

    可使用hasPrefix("")判断开头

    var str = "hello"
    print(str.hasPrefix("h")) // true
    print(str.hasPrefix("he")) // true
    print(str.hasPrefix("e")) // false

    使用hasSuffix()判断结尾

    var str = "hello"
    print(str.hasPrefix("o")) // true
    print(str.hasPrefix("lo")) // true
    print(str.hasPrefix("ol")) // false

2. 增

  • 字符串结尾增加新字符串

    append()即可:

    var str = "hello"
    str.append(" world") // hello world
  • 在某个位置增加某段字符串

    insert(contentsOf: str, at: String.Index)可以在at位置的前面插入str:

    var str = "hello"
    str.insert(contentsOf: "AAA", at: str.startIndex) // AAAhello

3. 改

  • 替换某一字段

    replaceSubrange(section, with: "lalala") section是String.Index的区间范围,替换为"lalala":

    var str = "hello"
    let section = str.startIndex...str.index(str.endIndex, offsetBy: -2)
    str.replaceSubrange(section, with: "lalala") // "lalalao"

    也可以使用replacingOccurrences(of: str, with: "jj")将str字段替换为"jj":

    var str = "hello"
    str.replacingOccurrences(of: "ll", with: "jj") // "hejjo"

    如果没有该字段,则不替换

4. 删

  • 删除某位置的字符

    remove(at: String.Index)

    var str = "hello"
    str.remove(at: str.index(str.startIndex, offsetBy: 2)) // l
    print(str) // helo
  • 删除某字段

    removeSubrange(String.Index...String.Index)

    var str = "hello"
    str.removeSubrange(str.startIndex...str.index(str.endIndex, offsetBy: -2))
    print(str) // o

04 利用for循环遍历字符串

  • 直接遍历

    var str = "hello"
    for item in str {
    print(item)
    }
  • 使用index()方法来遍历

    var str = "hello"
    for item in 0..<str.count {
    print(str[str.index(str.startIndex, offsetBy: item)])
    }

Swift初探03 字符串操作的更多相关文章

  1. .NET面试题解析(03)-string与字符串操作

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...

  2. Swift初探01 变量与控制流

    Swift初探01 变量与控制流 输出"hello world"是几乎学习所有编程语言的第一课,这是程序员的情怀. 所以我们学习swift的第一步,就是输出一句"Hell ...

  3. Python 字符串操作及string模块使用

    python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对 ...

  4. 李洪强iOS开发Swift篇—03_字符串和数据类型

    李洪强iOS开发Swift篇—03_字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容  let website = "http:// ...

  5. 初探Java字符串

    转载: 初探Java字符串 String印象 String是java中的无处不在的类,使用也很简单.初学java,就已经有字符串是不可变的盖棺定论,解释通常是:它是final的. 不过,String是 ...

  6. mysql之字符串操作

    写在前面 上篇文章学习了mysql常用的日期操作的函数,这篇文章将学习mysql的字符串操作的函数. 系列文章 mysql之创建数据库,创建数据表 mysql之select,insert,delete ...

  7. VC++ 字符串操作学习总结

    vc++中各种字符串(转载) http://www.cnblogs.com/tomin/archive/2008/12/28/1364097.html CString ,BSTR ,LPCTSTR之间 ...

  8. OMG,12 个精致的 Java 字符串操作小技巧,学它

    字符串可以说是 Java 中最具有代表性的类了,似乎没有之一哈,这就好像直播界的李佳琪,脱口秀中的李诞,一等一的大哥地位.不得不承认,最近吐槽大会刷多了,脑子里全是那些段子,写文章都有点不由自主,真的 ...

  9. python学习笔记(字符串操作、字典操作、三级菜单实例)

    字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...

随机推荐

  1. HttpServletRequest.getInputStream()多次读取问题

    转自:https://www.jianshu.com/p/85feeb30c1ed HttpServletRequest.getInputStream()多次读取问题   背景 使用POST方法发送数 ...

  2. Flask-Script使用教程

    Flask使用第三方脚本 一个干净的项目准备: 一个干净的Flask项目连接地址: https://pan.baidu.com/s/123TyVXOFvh5P7V8MbyMfDg 话不多说,上菜: 1 ...

  3. Eclipse 从SVN检出项目之《文件夹 “” 已不存在 》

    1.eclipse 从svn检出项目 报文件夹不存在, 参考博客 https://blog.csdn.net/wenbsu/article/details/80965680 2.You need to ...

  4. fsdfd

    static int kWeiOfVal(int val, int k) { int n = 1; int temVal = val; int result; while (1) { temVal = ...

  5. HTML5标签速查

    HTML5标签速查,助你快速了解HTML 5. HTML 5新加入的标签以黑体标识,HTML 5不支持的以斜体标识. 标签 描述 <!--...--> 评论 <!DOCTYPE> ...

  6. 手把手教你从零写一个简单的 VUE--模板篇

    教程目录1.手把手教你从零写一个简单的 VUE2.手把手教你从零写一个简单的 VUE--模板篇 Hello,我又回来了,上一次的文章教会了大家如何书写一个简单 VUE,里面实现了VUE 的数据驱动视图 ...

  7. WPF控件大全(表格)-学习总结

    Label标签 label控件:一般用户描述性文字显示. 在Label控件使用时,一般给予用户提示.用法上没有什么很特殊的,label控件的值记住:不是Text 而是 Content属性. TextB ...

  8. hql常用查询语句

    // HQL: Hibernate Query Language.// 特点:// >> 1,与SQL相似,SQL中的语法基本上都可以直接使用.// >> 2,SQL查询的是表 ...

  9. Linux_文件传输工具_FileZilla

    什么是FileZilla? FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版本,具备所有的FTP软件功能.可控性.有条理的界面和管理多站点的简化方式使得Filezilla客户端版 ...

  10. Mybatis-Dao层实现(通过代理方式)

    1.代理方式开发是主流 2.Mapper接口开发方法只需要编写Mapper接口(相当于Dao接口),然后由Mybatis根据接口创建动态代理对象 Mapper接口开发需要遵循以下规范 一一对应 Use ...