gpt4 book ai didi

swift - 如何搜索一系列字符串

转载 作者:行者123 更新时间:2023-11-30 11:44:23 25 4
gpt4 key购买 nike

如何搜索一系列字符串,我要搜索用户ID

但是这次的userID可能是“123”,下一次是“zxvcvb”,所以我不能使用 offsetBy

let userID = "12345"
let URL = "http://test/main/?Username=\(userID)#!/index.php"
let firstIndex = URL.index(of: "=")
let secondIndex = URL.index(of: "#")
let range = firstIndex...secondIndex //error

最佳答案

试试这个代码:

let userID = "jshjdschd"
let url = "http://test/main/?Username=\(userID)#!/index.php"

guard let firstIndex = url.index(of: "="),
let secondIndex = url[firstIndex...].index(of: "#") else{
print("UserId not found")
}

let range = url.index(after: firstIndex)..<secondIndex
let mySubstring = url[range]
print(mySubstring) //jshjdschd

关于swift - 如何搜索一系列字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com