What is the best way to determine if a string contains a character from a set in Swift

前端 未结 11 1687
南旧
南旧 2020-11-30 01:03

I need to determine if a string contains any of the characters from a custom set that I have defined.

I see from this post that you can use rangeOfString to determine

相关标签:
11条回答
  • 2020-11-30 01:48

    Using Swift 3 to determine if your string contains characters from a specific CharacterSet:

        let letters = CharacterSet.alphanumerics
        let string = "my-string_"
        if (string.trimmingCharacters(in: letters) != "") {
            print("Invalid characters in string.")
        }
        else {
            print("Only letters and numbers.")
        }
    
    0 讨论(0)
  • 2020-11-30 01:52

    ONE LINE Swift4 solution to check if contains letters:

    CharacterSet.letters.isSuperset(of: CharacterSet(charactersIn: myString) // returns BOOL
    

    Another case when you need to validate string for custom char sets. For example if string contains only letters and (for example) dashes and spaces:

    let customSet: CharacterSet = [" ", "-"]
    let finalSet = CharacterSet.letters.union(customSet)
    finalSet.isSuperset(of: CharacterSet(charactersIn: myString)) // BOOL
    

    Hope it helps someone one day:)

    0 讨论(0)
  • 2020-11-30 01:53

    Swift 4

    let myString = "Some Words"
    
    if myString.contains("Some"){
        print("myString contains the word `Some`")
    }else{
        print("myString does NOT contain the word `Some`")
    }
    

    Swift 3:

    let myString = "Some Words"
    
    if (myString.range(of: "Some") != nil){
        print("myString contains the word `Some`")
    }else{
        print("Word does not contain `Some`")
    }
    
    0 讨论(0)
  • 2020-11-30 01:54

    You could do it this way:

    var string = "Hello, World!"
    
    if string.rangeOfString("W") != nil {
         println("exists")
    } else {
         println("doesn't exist")
    }
    
    // alternative: not case sensitive
    if string.lowercaseString.rangeOfString("w") != nil {
         println("exists")
    } else {
         println("doesn't exist")
    }
    
    0 讨论(0)
  • 2020-11-30 01:54

    Updated for Swift 5.1

    Question was specifically: "if a string contains any of the characters from a custom set" and not checking for all characters in the custom set.

    Using

    func check(in string: String, forAnyIn characters: String) -> Bool {
        // create one character set
        let customSet = CharacterSet(charactersIn: characters)
        // use the rangeOfCharacter(from: CharacterSet) function
        return string.rangeOfCharacter(from: customSet) != nil 
    }
    
    check(in: "abc", forAnyIn: "A") // false
    check(in: "abc", forAnyIn: "b") // true
    
    

    But this is also very easy to check using character sets.

    
    func check(in string: String, forAnyIn characters: String) -> Bool {
        let customSet = CharacterSet(charactersIn: characters)
        let inputSet = CharacterSet(charactersIn: string)
        return !inputSet.intersection(customSet).isEmpty
    }
    
    check(in: "abc", forAnyIn: "A") // false
    check(in: "abc", forAnyIn: "b") // true
    
    
    0 讨论(0)
提交回复
热议问题