字符串操作

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. mybatis源码之我见

    以前一直想看mybatis的源代码,但是一直没找到入口(傻),最近看教程,有些感悟. 和起以前一样,关键代码我会用红色标记. 首先,先贴下我的dao和mapper,代码很简单,和平时写的hello w ...

  2. java中的VO、PO、BO、DAO、POJO

     针对java工程里的各种带O的对象,进行分析,了解各自的作用. PO: persistent object,持久对象.与数据库里表字段一一对应.PO是一些属性,以及set和get方法组成.一般情况下 ...

  3. 乱序数组中第k大的数(顺序统计量)

    该问题是顺序统计量中十分经典的问题. 使用快排中的分区法,将第k大的数排序.若双向扫描分区加上三点中值法或绝对中值法,可以保证在 O(n) 时间里找出第k大的数. 补充:可以直接使用C++STL中的n ...

  4. SVN在拉取(更新)代码的时候出现Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted问题 ---window版

    简易方法1 今天朋友看到朋友报错这个错误,偷偷学习了下他的方法并做记录以防忘记 简易方法2 今天使用svn时报了一个这个错,网上搜索时都说是要使用sqllite来删除svn队列. 其实可以直接使用id ...

  5. git提交错误 git config --global user.email “you@example.com“ git config --global user.name “Your Name

    1 Commit failed - exit code 128 received, with output: '*** Please tell me who you are. 2 3 Run 4 5 ...

  6. vue全家桶+axios+jsonp+es6 仿肤君试用小程序

    vue全家桶+axios+jsonp+es6 仿肤君试用小程序 把自己写的一个小程序项目用vue来实现的,代码里面有一些注释,主要使用了vue-cli,vue,vuex,vue-router,axoi ...

  7. java中final变量的用法

    4.4 final变量    final变量的数值不能在初始化之后进行改变(你希望a=3,有很多用到a的场合, 你当然不能在程序中就用3来代替a). 比如: final int h = 0; 想像有一 ...

  8. CCF201604-2俄罗斯方块

    问题描述 俄罗斯方块是俄罗斯人阿列克谢·帕基特诺夫发明的一款休闲游戏. 游戏在一个15行10列的方格图上进行,方格图上的每一个格子可能已经放置了方块,或者没有放置方块.每一轮,都会有一个新的由4个小方 ...

  9. Python程序的流程

    1 """ 2 python程序的流程 3 """ 4 # ------------- 分支结构---------------- 5 # i ...

  10. LC-206

    206. 反转链表 迭代法 class Solution { public ListNode reverseList(ListNode head) { //申请节点,pre和 cur,pre指向nul ...