How to check if String is Number in Swift
source link: https://sarunw.com/posts/how-to-check-if-string-is-number-in-swift/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to check if String is Number in Swift
Table of Contents
There are many ways to check whether a string is a number in Swift.
Since people refer to a number differently, to get the right solution, we need to know what kind of number we are going to check.
I will classify a number into four categories.
- String with only digits, e.g., 0 to 9
- String with decimal numbers, e.g., ๙
- String represents a number, e.g., ⅚, 7, 𝟠
- String represents a whole number, e.g., 1, 万
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.
String with only digits
If you want to check whether a string contains only 0 to 9, the easiest method is explicitly check for those ten characters (0 to 9).
There might be many ways to do this, but I will show you two ways to do it.
CharacterSet
We declare a character set with all characters that we want to check. Then we check our string against that set.
extension String {
var isNumber: Bool {
let digitsCharacters = CharacterSet(charactersIn: "0123456789")
return CharacterSet(charactersIn: self).isSubset(of: digitsCharacters)
}
}
Regular Expression
You can also use regular expression for this.
We use range(of:options:)
method. This method finds and returns the range of the first occurrence of a given string within the string.
We provide a regular expression pattern as a search string and specify comparing options as .regularExpression
to make the method do a regular expression search.
extension String {
var isNumber: Bool {
return self.range(
of: "^[0-9]*$", // 1
options: .regularExpression) != nil
}
}
1^[0-9]*$
is a pattern that matches a string that starts and ends with the numbers 0 to 9.
Both methods yield the same result.
"0123456789".isNumber = true
"๑๒๓๔๕๖๗๘๙".isNumber = false
"⅚".isNumber = false
"㊈".isNumber = false
"𝟠".isNumber = false
"万".isNumber = false
"1️⃣".isNumber = false
"123456.789".isNumber = false
"123,456,789".isNumber = false
"123,456.789".isNumber = false
"SwiftUI 2.0".isNumber = false
String with Decimal numbers
If you want to check for string contains any characters classified as Decimal Numbers, you can use the built-in character set, CharacterSet.decimalDigits
CharacterSet.decimalDigits
is the set of all characters used to represent the decimal values 0 through 9.
These character sets include a wider range of decimal digits.
For example:
- Decimal digits in other languages, e.g, ๑๒๓ (123 in Thai).
- Decimal digits used in mathematics, i.e., Doublestruck[1] style digits, e.g., 𝟠
I use the same technique in the previous section but with a different character set.
extension String {
var isNumber: Bool {
let characters = CharacterSet.decimalDigits
return CharacterSet(charactersIn: self).isSubset(of: characters)
}
}
Here is the result.
"0123456789".isNumber = true
"๑๒๓๔๕๖๗๘๙".isNumber = true
"⅚".isNumber = false
"㊈".isNumber = false
"𝟠".isNumber = true
"万".isNumber = false
"1️⃣".isNumber = false
"123456.789".isNumber = false
"123,456,789".isNumber = false
"123,456.789".isNumber = false
"SwiftUI 2.0".isNumber = false
String represents a number
There are many strings that can represent a number, for example, "⅚" (Fraction) and "㊈" (Circled Chinese nine).
Swift Character
has a built-in property, isNumber
, that can indicate whether the character represents a number or not.
We use this property along with allSatisfy
to check whether all the characters in a string are a number.
allSatisfy
returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.
extension String {
var isNumber: Bool {
return self.allSatisfy { character in
character.isNumber
}
}
}
As you can see isNumber
property can detect many Unicode characters representing a number.
"0123456789".isNumber = true
"๑๒๓๔๕๖๗๘๙".isNumber = true
"⅚".isNumber = true
"㊈".isNumber = true
"𝟠".isNumber = true
"万".isNumber = true
"1️⃣".isNumber = true
"123456.789".isNumber = false
"123,456,789".isNumber = false
"123,456.789".isNumber = false
"SwiftUI 2.0".isNumber = false
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.
String represents a whole number
Character
also has another property, isWholeNumber
, to test for a character that can represent a whole number.
This is similar to String represents a number but will contain fewer characters, e.g., "⅚" (fraction isn't a whole number).
extension String {
var isNumber: Bool {
return self.allSatisfy { character in
character.isWholeNumber
}
}
}
Here is the result.
"0123456789".isNumber = true
"๑๒๓๔๕๖๗๘๙".isNumber = true
"⅚".isNumber = false
"㊈".isNumber = true
"𝟠".isNumber = true
"万".isNumber = true
"1️⃣".isNumber = false
"123456.789".isNumber = false
"123,456,789".isNumber = false
"123,456.789".isNumber = false
"SwiftUI 2.0".isNumber = false
A letter of the alphabet drawn with doubled vertical strokes is called doublestruck, or sometimes blackboard bold (because doublestruck characters provide a means of indicating bold font weight when writing on a blackboard). For example, 𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡. https://mathworld.wolfram.com/Doublestruck.html ↩︎
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK