-1

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")
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 1
    Does this answer your question? [What is the best way to determine if a string contains a character from a set in Swift](https://stackoverflow.com/questions/28486138/what-is-the-best-way-to-determine-if-a-string-contains-a-character-from-a-set-in) – rbaldwin Apr 05 '22 at 14:54
  • `string.contains(where: ("0"..."9").contains)` – Leo Dabus Apr 05 '22 at 16:15

1 Answers1

0

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
vadian
  • 274,689
  • 30
  • 353
  • 361