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

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

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 if a string contains another string. This, of course, also works for characters if you test each character one at a time.

I’m wondering what the best way to do this is.

Answers:

You can create a CharacterSet containing the set of your custom characters
and then test the membership against this character set:

Swift 3:

let charset = CharacterSet(charactersIn: "aw")
if str.rangeOfCharacter(from: charset) != nil {
    print("yes")
}

For case-insensitive comparison, use

if str.lowercased().rangeOfCharacter(from: charset) != nil {
    print("yes")
}

(assuming that the character set contains only lowercase letters).

Swift 2:

let charset = NSCharacterSet(charactersInString: "aw")
if str.rangeOfCharacterFromSet(charset) != nil {
    print("yes")
}

Swift 1.2

let charset = NSCharacterSet(charactersInString: "aw")
if str.rangeOfCharacterFromSet(charset, options: nil, range: nil) != nil {
    println("yes")
}

Questions:
Answers:

From Swift 1.2 you can do that using Set

var str = "Hello, World!"
let charset: Set<Character> = ["e", "n"]

charset.isSubsetOf(str)     // `true` if `str` contains all characters in `charset`
charset.isDisjointWith(str) // `true` if `str` does not contains any characters in `charset`
charset.intersect(str)      // set of characters both `str` and `charset` contains.

Questions:
Answers:

swift3

var str = "Hello, playground"
let res = str.characters.contains { ["x", "z"].contains( $0 ) }

Questions:
Answers:

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")
}

Questions:
Answers:
func isAnyCharacter( from charSetString: String, containedIn string: String ) -> Bool
{
    return Set( charSetString.characters ).isDisjointWith( Set( string.characters) ) == false
}

Questions:
Answers:

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`")
}

Questions:
Answers:

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.")
    }

Questions:
Answers:

Swift 3 example:

extension String{
    var isContainsLetters : Bool{
        let letters = CharacterSet.letters
        return self.rangeOfCharacter(from: letters) != nil
    }
}

Usage :

"123".isContainsLetters // false