[英]Swift, how can I check a string for the presence of characters from the list in it?
I have a string string and I need to determine if one of the characters from the Correct List string is in it?我有一个字符串字符串,我需要确定其中是否包含正确列表字符串中的字符之一? How do I do this?我该怎么做呢?
Maybe you need to change the Correct List type from String to List?也许您需要将正确的列表类型从字符串更改为列表?
import SwiftUI
struct test: View {
var string: String = ""
var correctList: String = ""
var body: some View {
Text(string)
}
}
struct test_Previews: PreviewProvider {
static var previews: some View {
test(string: "Qwerty1", correctList: "1234567890")
}
}
You can check against an arbitrary set of characters您可以检查任意一组字符
let characterSet = CharacterSet(charactersIn: "1234567890")
let isValid = string.rangeOfCharacter(from: characterSet) != nil
or with Regular Expression, the set of characters must be wrapped in square brackets.或者使用正则表达式,字符集必须用方括号括起来。
let string = "Qwerty1"
let containsNumber = string.range(of: "[1234567890]", options: .regularExpression) != nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.