一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
Swift Extension Substring字符串扩展方法(源码)
效果
封装是对字符串的拼接
和截取
操作
使用
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 获取字符串的索引
let num_0 = "15989954385"
if let index = num_0.getIndexOf("3") {
print("1字符 '3' 的索引是: \(index)") // 输出: 字符 '3' 的索引是: 8
}
let num_1 = "15989954385"
print("2字符串的长度是: \(num_1.length)") // 输出: 字符串的长度是: 11
let num_2 = "15989954385"
let newNum = num_2.append("ok")
print("3拼接后的字符串是: \(newNum)") // 输出: 拼接后的字符串是: 15989954385ok
let num_3 = "15989954385"
let substring = num_3[2..<5]
print("4截取的子串是: \(substring)") // 输出: 截取的子串是: 989
let num_4 = "15989954385"
let substring_4 = num_4[2...5]
print("5截取的子串是: \(substring_4)") // 输出: 截取的子串是: 9899
let num_5 = "15989954385"
let substring_5 = num_5.substring(from: 2, to: 5)
print("6截取的子串是: \(substring_5)") // 输出: 截取的子串是: 9899
let num_6 = "15989954385"
let substring_6 = num_6.substringInclude(front: 5)
print("7截取的子串是: \(substring_6)") // 输出: 截取的子串是: 159899
let num_7 = "15989954385"
let substring_7 = num_7.substringInclude(behind: 5)
print("8截取的子串是: \(substring_7)") // 输出: 截取的子串是: 954385
let num_8 = "15989954385"
let substring_8 = num_8.substringNoInclude(front: 5)
print("9截取的子串是: \(substring_8)") // 输出: 截取的子串是: 15989
let num_9 = "15989954385"
let substring_9 = num_9.substringNoInclude(behind: 5)
print("10截取的子串是: \(substring_9)") // 输出: 截取的子串是: 54385
let num_10 = "15989954385"
let substring_10 = num_10.substringInclude(front: "99")
print("11截取的子串是: \(substring_10)") // 输出: 截取的子串是: 1598
let num_11 = "15989954385"
let substring_11 = num_11.substringInclude(behind: "99")
print("12截取的子串是: \(substring_11)") // 输出: 截取的子串是: 954385
let url = "https://example.com/search?q=swift programming"
let encodedUrl_0 = url.urlEncoded()
print("13编码后的URL: \(encodedUrl_0)") // 输出: 编码后的URL: https://example.com/search?q=swift%20programming
let encodedUrl_1 = "https://example.com/search?q=swift%20programming"
let decodedUrl_2 = encodedUrl_1.urlDecoded()
print("14解码后的URL: \(decodedUrl_2)") // 输出: 解码后的URL: https://example.com/search?q=swift programming
let values = "apple,banana,orange"
let list = values.stringForList(values: values, splitSymbol: ",")
print("15分割后的数组: \(list)") // 输出: 分割后的数组: ["apple", "banana", "orange"]
}
}