swift3.0字符串截取,字符串处理
2017-07-30 14:55
417 查看
1.截取前面三个字符串,要求输出结果为abc
let str ="abcdefghi"
let index = str.index(str.startIndex, offsetBy:3)//获取字符d的索引
let result = str.substring(to: index)//从起始截取到索引的所有字符串,即abc,注意不包含d
打印结果为 abc
2.截取第四个开始的所有字符串,要求输出结果为defghi
let str = "abcdefghi"
let index = str.index(str.startIndex, offsetBy: 3)//获取字符d的索引
let result = str.substring(from: index)//从d的索引开始截取后面所有的字符串即defghi
打印结果为defghi
3.截取第4个开始的长度为2个字符串,要求结果输出为de
let str ="abcdefghi"
let startIndex = str.index(str.startIndex, offsetBy:3)//获取d的索引
let endIndex = str.index(startIndex, offsetBy:2)//从d的索引开始往后两个,即获取f的索引
let result = str.substring(with: startIndex..<endIndex)//输出d的索引到f索引的范围,注意..<符号表示输出不包含f
打印结果为de
4.判断字符串是否包含某段字符
let str =
"abcdefghi"
let result = str.contains("ac")
打印结果为false
let result = str.contains("abc")
打印结果为true
5.字符串大小写转换
let str =
"ABCDEFGHI"
let result = str.lowercased()
输出结果为abcdefghi
let str =
"abcdefghi"
let result = str.uppercased()
输出结果为ABCDEFGHI
6.指定字符串替换为其他字符串,将abc替换成m,使输出结果为mdefghi
let str =
"abcdefghi"
let result = str.replacingOccurrences(of:"abc", with:
"m")
输出结果为mdefghi
7.指定范围内的字符串替换为其他字符串,将cde替换成m,使输出结果为abmfghi
let str =
"abcdefghi"
let startIndex = str.index(str.startIndex, offsetBy:2)
let endIndex = str.index(startIndex, offsetBy:3)
let result = str.replacingCharacters(in: startIndex..<endIndex, with:"m")
输出结果为abmfghi
8.有时候不确定字符串的长度,需求为截取字符串末尾面开始往前5位,截取长度为2的字符串,使输出为ef
let str =
"abcdefghi"
let startIndex = str.index(str.startIndex, offsetBy: str.lengthOfBytes(using:
.utf8) - 5)
let endIndex = str.index(startIndex, offsetBy:2)
let result = str.substring(with: startIndex..<endIndex)
输出结果为ef
9. 字符串转为数组,使得输出为["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let str =
"a.b.c.d.e.f.g.h.i"
let result = str.components(separatedBy:".")
输出为["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let str ="abcdefghi"
let index = str.index(str.startIndex, offsetBy:3)//获取字符d的索引
let result = str.substring(to: index)//从起始截取到索引的所有字符串,即abc,注意不包含d
打印结果为 abc
2.截取第四个开始的所有字符串,要求输出结果为defghi
let str = "abcdefghi"
let index = str.index(str.startIndex, offsetBy: 3)//获取字符d的索引
let result = str.substring(from: index)//从d的索引开始截取后面所有的字符串即defghi
打印结果为defghi
3.截取第4个开始的长度为2个字符串,要求结果输出为de
let str ="abcdefghi"
let startIndex = str.index(str.startIndex, offsetBy:3)//获取d的索引
let endIndex = str.index(startIndex, offsetBy:2)//从d的索引开始往后两个,即获取f的索引
let result = str.substring(with: startIndex..<endIndex)//输出d的索引到f索引的范围,注意..<符号表示输出不包含f
打印结果为de
4.判断字符串是否包含某段字符
let str =
"abcdefghi"
let result = str.contains("ac")
打印结果为false
let result = str.contains("abc")
打印结果为true
5.字符串大小写转换
let str =
"ABCDEFGHI"
let result = str.lowercased()
输出结果为abcdefghi
let str =
"abcdefghi"
let result = str.uppercased()
输出结果为ABCDEFGHI
6.指定字符串替换为其他字符串,将abc替换成m,使输出结果为mdefghi
let str =
"abcdefghi"
let result = str.replacingOccurrences(of:"abc", with:
"m")
输出结果为mdefghi
7.指定范围内的字符串替换为其他字符串,将cde替换成m,使输出结果为abmfghi
let str =
"abcdefghi"
let startIndex = str.index(str.startIndex, offsetBy:2)
let endIndex = str.index(startIndex, offsetBy:3)
let result = str.replacingCharacters(in: startIndex..<endIndex, with:"m")
输出结果为abmfghi
8.有时候不确定字符串的长度,需求为截取字符串末尾面开始往前5位,截取长度为2的字符串,使输出为ef
let str =
"abcdefghi"
let startIndex = str.index(str.startIndex, offsetBy: str.lengthOfBytes(using:
.utf8) - 5)
let endIndex = str.index(startIndex, offsetBy:2)
let result = str.substring(with: startIndex..<endIndex)
输出结果为ef
9. 字符串转为数组,使得输出为["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let str =
"a.b.c.d.e.f.g.h.i"
let result = str.components(separatedBy:".")
输出为["a", "b", "c", "d", "e", "f", "g", "h", "i"]
相关文章推荐
- [Swift 3.0]String的Extension--字符串的截取
- Swift 3.0 字符串的处理
- Swift3.0语言教程分割字符串与截取字符串
- swift 3.0 字符串截取
- Swift3.0语言教程分割字符串与截取字符串
- swift 3.0中实现字符串截取、比较的方法示例
- swift3.0中String.Index截取字符串方法,很方便哦
- 【iOS】Swift字符串截取方法的改进
- Swift3.0——字符串与基本运算符中的变化
- 批处理BAT替换与截取字符串的用法t1=%a:~3%是什么意思
- datagrid的文字换行与连续字符串换行处理,字符串三种截取方式
- PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
- Swift字符串操作(截取、替换)String
- EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
- 批处理学习--字符串截取
- iOS NSString 字符串处理:截取字符串、匹配字符串、分隔字符串
- BAT批处理中的字符串处理详解(字符串截取)
- EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
- Swift3.0带来的变化汇总系列一——字符串与基本运算符中的变化
- DOS批处理中的字符串处理详解(字符串截取)