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 ...
随机推荐
- 客户端注册 Watcher 实现?
1.调用 getData()/getChildren()/exist()三个 API,传入 Watcher 对象 2.标记请求 request,封装 Watcher 到 WatchRegistrati ...
- H5扇形
使用H5 canvas绘制的可交互扇形 requestAnimationFrame() 现有动画实现方式的不足 setTimeout和setInterval都不十分精确.为它们传入的第二个参数,实际上 ...
- oracle 序列的使用
序列: 是oacle提供的用于产生一系列唯一数字的数据库对象. 自动提供自增的唯一的数值. 共享的数据 主要用于提供主键值 将序列装入内存可以提高访问效率 1.创建序列: 1. 要有创建序列的权限 ...
- CCS基础样式表
一.css样式表 1.样式表分类 1.内联式 <p >This is an apple</p> 2.内嵌样式表 作为一个独立的区域 内嵌在网页里面,必须写在head标签里面 & ...
- vuecli中配置webpack加快打包速度
webpack4中webpack 的DllPlugin插件可以将常见的库文件作为dll文件来,每次打包的时候就不用再次打包库文件了. 但是游鱼西在vuecli中已经去除这个选项,意识到带来的打包速度提 ...
- css3属性之filter初探
filter属性是css不常用的一个属性,但是用好了可以给网页增色不少!ps: IE不支持此属性: img { -webkit-filter: grayscale(100%); /* Chrome, ...
- SpringMVC基于注解开发的步骤
基于xml配置 .1准备好以下相关jar包 .2创建Maven项目使用骨架 (这里选择第二个以webapp结尾的非第一个) 给项目起个名字 这里可以更改maven本地仓库(依赖包所存放的地方)的路径 ...
- show binary logs
列出服务器上的二进制日志文件.该语句用作" purge binary logs语句"中描述的过程的一部分,该过程显示了如何确定可以清除哪些日志. show binary logs ...
- STL空间配置器源码分析(四)bitmap_allocator
一.摘要 bitmap_allocator是STL空间分配器的其中一种,它采用内存池策略,最多存储64条空闲链表(freelist,实际是一块空间连续的内存区,后面也称为超级块),每条空闲链表存储的内 ...
- JavaScript学习总结1-字符、数字
1.严格检查模式 JavaScript是一种十分随便自由的语言 1 <script> 2 console.log(i); 3 </script> 即使没有定义i变量,也能在控制 ...