How to check if String is Number in Swift

⋅ 4 min read ⋅ 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.

  1. String with only digits, e.g., 0 to 9
  2. String with decimal numbers, e.g., ๙
  3. String represents a number, e.g., ⅚, 7, 𝟠
  4. String represents a whole number, e.g., 1, 万

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.

  1. CharacterSet
  2. Regular expression

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

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

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.

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

  1. 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 ↩︎


Read more article about Swift or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
viewDidLoad() in SwiftUI

Learn how to simulate the viewDidLoad() behavior in SwiftUI using the onAppear modifier.

Next
How to create Rounded Corners Button in UIKit

There are many ways to create a rounded corners button in UIKit based on the minimum iOS version you want to support. Let's learn how to do it.

← Home