Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a String is alphanumeric in Swift

In Swift, how can I check if a String is alphanumeric, ie, if it contains only one or more alphanumeric characters [a-zA-Z0-9], excluding letters with diacritics, eg, é.

like image 557
ma11hew28 Avatar asked Mar 14 '16 16:03

ma11hew28


People also ask

How do I check if a string is alphanumeric Swift?

In Swift, how can I check if a String is alphanumeric, ie, if it contains only one or more alphanumeric characters [a-zA-Z0-9] , excluding letters with diacritics, eg, é.

How to check substring in string in Swift?

Checking if a string contains a substring in Swift is very easy. Strings in Swift have a built in method called contains . This method with allow us to check if a string contains a given substring.

What is substring in Swift?

Substrings. When you get a substring from a string—for example, using a subscript or a method like prefix(_:) —the result is an instance of Substring , not another string. Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings.


1 Answers

extension String {     var isAlphanumeric: Bool {         return !isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil     } }  "".isAlphanumeric        // false "abc".isAlphanumeric     // true "123".isAlphanumeric     // true "ABC123".isAlphanumeric  // true "iOS 9".isAlphanumeric   // false 
like image 123
ma11hew28 Avatar answered Sep 28 '22 05:09

ma11hew28