我有一个文本字段,用户可以用它来输入他们的 LinkedIn URL。但是,我不希望用户能够输入他们想要的任何 URL,所以我如何检查他们输入的字符串是否完全以
开头 https://www.linkedin.com/in/...
最佳答案
您可以使用 String 的 hasPrefix 函数:
if textField.text.hasPrefix("https://www.linkedin.com/in") {
print("string starts with https://www.linkedin.com/in")
} else {
print("string does not start with https://www.linkedin.com/in")
}
关于swift - 在 Xcode 中检查 URL 文本作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784236/