Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

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.

like image 652
mattnedrich Avatar asked Feb 12 '15 19:02

mattnedrich


People also ask

How do you check a string contains a character in Swift?

Swift String contains() The contains() method checks whether the specified string (sequence of characters) is present in the string or not.

How do you check if a string contains a set of characters?

While the find and count string methods can check for substring occurrences, there is no ready-made function to check for the occurrence in a string of a set of characters. While working on a condition to check whether a string contained the special characters used in the glob.

How do you check if a string contains a set of characters in Java?

The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement.

How do you check if a string is capitalized in Swift?

Add this Swift extension so that all instances of Character class have two new functions: isUpperCase() to test if the character is upper case and isLowerCase() to test if the character is lower case.


2 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") } 
like image 50
Martin R Avatar answered Oct 12 '22 19:10

Martin R


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:)

like image 41
Tung Fam Avatar answered Oct 12 '22 17:10

Tung Fam