我有下面的字符串,我想删除此部分“https://drive.google.com/open?id=”,以便我的字符串只有“1FN7S3N_9IAkWYMjLnqh4BKsh8_YT0Zma”任何建议?谢谢!
let string = "https://drive.google.com/open?id=1FN7S3N_9IAkWYMjLnqh4BKsh8_YT0Zma"
答案 0 :(得分:1)
URL可以有其他参数,所以只是替换字符串可能不是一个好的解决方案。 URLComponents
为此提供了便利。
func getQueryStringParameter(url: String, param: String) -> String? {
return URLComponents(string: url)?.queryItems?.first{ $0.name == param }?.value
}
并像这样使用它。
let string = "https://drive.google.com/open?id=1FN7S3N_9IAkWYMjLnqh4BKsh8_YT0Zma"
if let id = getQueryStringParameter(url: string, param: "id") {
print(id)
}
答案 1 :(得分:0)
将第二个组件分隔为谷歌驱动器部件:
let string = "https://drive.google.com/open?id=1FN7S3N_9IAkWYMjLnqh4BKsh8_YT0Zma"
let id = string.components(separatedBy: "https://drive.google.com/open?id=")[1]
将 google drive part 替换为空字符串:
let string = "https://drive.google.com/open?id=1FN7S3N_9IAkWYMjLnqh4BKsh8_YT0Zma"
let id = string.replacingOccurrences(of: "https://drive.google.com/open?id=", with: "")