iOS swift从字符串中删除前n个字符?

150 阅读1分钟
本人亲测有效!更多交流可以家魏鑫:lixiaowu1129,公重好:iOS过审汇总,一起探讨iOS技术!

第一种:

import Foundation

let url = URL(string: "http://google.com")!

var string = url.absoluteString
if let scheme = url.scheme, string.hasPrefix(scheme) {
    string.removeFirst(scheme.count + "://".count)
}

print(string)

第二种:

var str = "https://www.google.de"
if let httpRange = str.range(of: "https?://", options: .regularExpression, range: nil, locale: nil) {
    str.removeSubrange(httpRange)
}

或者:

let string = "https://www.google.de"
let newString = str.replacingOccurrences(of: "https?://", with: "", options: .regularExpression, range: nil)

本文由mdnice多平台发布