1.String的切割
例如:
let name: String = "zhangsan"
// 1.获取下标,从开始位置'z',往后移动一个位置,就是'h'的位置了
let subIndex = name.index(name.startIndex, offsetBy: 1)
let subIndex2 = name.index(name.startIndex, offsetBy: 4)
// 2.根据下标获取某个字符
let subStr = name[subIndex] //Character类型: h
// 3.获取中间某部分字符串
let midStr = name[subIndex...subIndex2] // hang
// 4.获取前面某部分
let preStr = name[...subIndex] // zh
// 5.获取后面某部分
let posStr = name[subIndex...] // hangsan
吐槽,实在是太难用了,我们写个扩展吧
extension String {
func mySubString(_ index1: Int, _ index2: Int) -> String {
let subIndex1 = self.index(self.startIndex, offsetBy: index1)
let subIndex2 = self.index(self.startIndex, offsetBy: index2)
return String(self[subIndex1...subIndex2])
}
func getCharAt(_ index: Int) -> Character {
let subIndex = self.index(self.startIndex, offsetBy: index)
return self[subIndex]
}
}
使用:
let name: String = "zhangsan"
let str1 = name.mySubString(0, 1)
let char1 = name.getCharAt(2)
print("char1",char1) // char1 a
print("str1",str1) // str1 zh
2.Character和Int互相转换
let c: Character = "B
let value = Int(c.asciiValue!)
print(value) // 66
let num = 66
let char: Character = Character(UnicodeScalar(num)!)
print(char) // B
let value: UInt32 = String(subStr).unicodeScalars.first!.value
吐槽,这也太啰嗦了吧,自己写个扩展
extension Character {
func toInt() -> Int {
return Int(String(self).unicodeScalars.first!.value)
}
}
使用:
let value2 = subStr.toInt()
3.String 和 Array的转换
在一些场景中,操作String,不如操作Array方便,操作完之后,再转回String
let arr = Array(name)
print(arr)
let arrStr = String(arr[0...4])
print(arrStr)
4. String的补充操作
a> 根据下标,截取子字符
b> 移除第一个子字符
c> 移除所有的子字符
d> 是否包含某个子字符
e> 重复的最多的字母
extension String {
/**
根据下标,截取子字符
*/
func subStr(_ start: Int, _ end: Int) -> String {
let startIdx = self.index(self.startIndex, offsetBy: start)
let endIdx = self.index(self.startIndex, offsetBy: end)
return String(self[startIdx...endIdx])
}
/**
移除第一个子字符
*/
func removeFirstSubStr(_ subStr: String) -> String {
if subStr.count > self.count {
return self
}
let len = subStr.count
for i in 0..<self.count {
let start = i
let end = i + len - 1
if end > self.count - 1 { // 防止数组越界
break
}
if subStr == self.subStr(start, end) {
if i == 0 && end == self.count - 1 { // 前后完全覆盖
return ""
} else if i == 0 && end < self.count - 1 { // 从0开始覆盖前面一部分
return self.subStr(end + 1, self.count - 1)
} else if i > 0 && end == self.count - 1 { // 从结尾覆盖后面一部分
return self.subStr(0, i - 1)
} else { // 中间
let preStr = self.subStr(0, i - 1)
let trailStr = self.subStr(end + 1, self.count - 1)
return preStr + trailStr
}
}
}
return self
}
/**
移除所有的子字符
*/
func removeAllSubStr(_ subStr: String) -> String {
var temp = self
while temp.isContainSubStr(subStr) {
temp = temp.removeFirstSubStr(subStr)
}
return temp
}
/**
是否包含某个子字符
*/
func isContainSubStr(_ subStr: String) -> Bool {
let len = subStr.count
for i in 0..<self.count {
let start = i
let end = i + len - 1
if end > self.count - 1 {
break
}
if subStr == self.subStr(start, end) {
return true
}
}
return false
}
/**
重复的最多的字母
*/
func getRepeatMaxCountLetter() -> Character {
let tempArr = self.components(separatedBy: " ").joined() // 去除空格再拼接字母
let dict = tempArr.reduce(into: [:]) { partialResult, char in
partialResult[char, default: 0] += 1
}
print("dict",dict)
var res: Character = "a"
var cnt = 0
dict.map { (key, value) in
if value > cnt {
cnt = value
let temp1 = key
res = temp1
}
}
return res
}
}
var str = "hello world hello jack"
let sub1Str = str.subStr(1, 3)
print(sub1Str) // ell
var res = str.removeFirstSubStr(sub1Str)
print(res) // ho world hello jack
var temp = "hello world hello jack hello world hello jack hello world hello jack"
let res1 = temp.removeAllSubStr(sub1Str)
print(res1) // ho world ho jack ho world ho jack ho world ho jack