Ios 如何拆分出现“quot”的字符串;用户/";从一个URL?
我正在使用此方法,这是我的URL“”。我想从这里得到“myYoutubeChennal”Ios 如何拆分出现“quot”的字符串;用户/";从一个URL?,ios,swift,Ios,Swift,我正在使用此方法,这是我的URL“”。我想从这里得到“myYoutubeChennal” func splitVideoUrl(videoUrl:String) -> String { //let token = videoUrl.split(separator: "user/") let ttstr:String = " https://www.youtube.com/user/myYoutubeChennal" if let range = ttstr.ran
func splitVideoUrl(videoUrl:String) -> String {
//let token = videoUrl.split(separator: "user/")
let ttstr:String = " https://www.youtube.com/user/myYoutubeChennal"
if let range = ttstr.range(of: "user/") {
let firstPart = String(describing: ttstr.startIndex..<range.lowerBound)
print(firstPart) // print Hello
return firstPart
}
return ""
}
func-splitVideoUrl(videoUrl:String)->String{
//let token=videoUrl.split(分隔符:“user/”)
让ttstr:String=”https://www.youtube.com/user/myYoutubeChennal"
如果let range=ttstr.range(of:“user/”){
让firstPart=String(描述:ttstr.startIndex..您可以使用以下代码获取URL的最后一个组件
let url = URL(string:"https://www.youtube.com/user/myYoutubeChennal")
let lastComponent = url?.lastPathComponent
假设所需字符串始终是user
后的路径组件,用斜杠拆分字符串,获取user
的索引,然后获取项目index+1
let string = "https://www.youtube.com/user/myYoutubeChennal"
let components = string.components(separatedBy: "/")
if let index = components.index(of: "user"), index < components.count - 1 {
let user = components[index + 1]
} else {
print("user not found")
}
let string=”https://www.youtube.com/user/myYoutubeChennal"
让components=string.components(以“/”分隔)
如果let index=components.index(属于“用户”),则index
请添加更多详细信息您想要什么?Thuan Nguyen请检查我现在已经编辑了它。这是一个URL,您想从中获取一个组件…我建议从类URLComponents
开始,不要将其视为字符串。好的,我知道了。谢谢您宝贵的时间。我的答案与miOS相同,请参考。对不起,我回信晚了,很高兴你帮了我。