Swift初探03 字符串操作
字符串操作
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 字符串操作的更多相关文章
- .NET面试题解析(03)-string与字符串操作
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...
- Swift初探01 变量与控制流
Swift初探01 变量与控制流 输出"hello world"是几乎学习所有编程语言的第一课,这是程序员的情怀. 所以我们学习swift的第一步,就是输出一句"Hell ...
- Python 字符串操作及string模块使用
python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对 ...
- 李洪强iOS开发Swift篇—03_字符串和数据类型
李洪强iOS开发Swift篇—03_字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容 let website = "http:// ...
- 初探Java字符串
转载: 初探Java字符串 String印象 String是java中的无处不在的类,使用也很简单.初学java,就已经有字符串是不可变的盖棺定论,解释通常是:它是final的. 不过,String是 ...
- mysql之字符串操作
写在前面 上篇文章学习了mysql常用的日期操作的函数,这篇文章将学习mysql的字符串操作的函数. 系列文章 mysql之创建数据库,创建数据表 mysql之select,insert,delete ...
- VC++ 字符串操作学习总结
vc++中各种字符串(转载) http://www.cnblogs.com/tomin/archive/2008/12/28/1364097.html CString ,BSTR ,LPCTSTR之间 ...
- OMG,12 个精致的 Java 字符串操作小技巧,学它
字符串可以说是 Java 中最具有代表性的类了,似乎没有之一哈,这就好像直播界的李佳琪,脱口秀中的李诞,一等一的大哥地位.不得不承认,最近吐槽大会刷多了,脑子里全是那些段子,写文章都有点不由自主,真的 ...
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
随机推荐
- 用maven建立一个工程4
在命令行里面输入cd C:\Users\admin\Documents\hello 然后按回车 再输入这行代码 mvn archetype:generate -DgroupId=com.liyongz ...
- 1108. IP 地址无效化
给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本. 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 ".". 示例 ...
- c语言中的字面量
在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation). 几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串: 而有很多也对布尔类 ...
- 根据地理信息绘画的html5 小游戏 - 简单实现
好久没写文章了,之前一直有一个想法,就是做一个根据用户行走的路线,获取地理位置,然后把它们绘制出来,最后产生的效果,类似蜗牛行走留下的痕迹. 最近思考了一下,搭了一个https,简单实现了一下,提供一 ...
- 在vue中创建多个ueditor实例
简介 在vue中创建多个ueditor实例,我使用neditor,其实就是把ueditor样式美化了下,其他和ueditor几乎一样 截图 源码地址 https://github.com/oblivi ...
- 纹理集打包和动画转换工具Texture Merge的使用教程
Texture Merger 可将零散纹理拼合为整图,同时也可以解析SWF.GIF动画,制作Egret位图文本,导出可供Egret使用的配置文件,其纹理集制作功能在小游戏开发中可以起到降低小游戏包体的 ...
- ES6-11学习笔记--类与继承
ES5 中的类与继承: 类的定义: function People(name, age) { // this指向当前实例化对象 console.log(this); // 实例属性 this.name ...
- 牛客网-剑指Offer 二维数组中的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- js判断时间格式不能超过30天
let first = this.data.date //开始时间 let second = e.detail.value //结束时间 var data1 = Date.parse(first.re ...
- 【云原生小课堂】高性能、高可用、可扩展的MySQL集群如何组建?
本期[云原生小课堂]将带你入门PXC--公认的MySQL集群优选方案.