Golang : How to check if a string with spaces in between is numeric?
Ok, I need a function that will determine if an input string is indeed numeric or not. The "standard way"
of checking if an input string is make up of digits is to use strconv.ParseFloat()
function. However, this method will fail if the input string has spaces in between.
Below is my own function that uses the unicode
package to determine if an input string is truly numeric or not.
Here you go!
package main
import (
"fmt"
"strconv"
"unicode"
)
func main() {
str1 := "123"
checkNumeric(str1)
fmt.Println(str1, IsNumeric(str1))
str2 := "12a3"
checkNumeric(str2)
fmt.Println(str2, IsNumeric(str2))
str3 := "abcd"
checkNumeric(str3)
fmt.Println(str3, IsNumeric(str3))
str4 := "12 3" // space will cause the "standard way" test to fail, trim space first if needed
checkNumeric(str4) // more robust, this is what I need
fmt.Println(str4, IsNumeric(str4)) // the "standard way" will mark this is false
str41 := "12 3 45 6 7"
checkNumeric(str41)
fmt.Println(str41, IsNumeric(str41))
str43 := "12 3 a 45 6 7"
checkNumeric(str43)
fmt.Println(str43, IsNumeric(str43))
str5 := "12.3"
checkNumeric(str5)
fmt.Println(str5, IsNumeric(str5))
str6 := "-123.4"
checkNumeric(str6)
fmt.Println(str6, IsNumeric(str6))
}
func checkNumeric(input string) {
var def bool = false
for _, v := range input {
if unicode.IsDigit(v) {
def = true
} else if unicode.IsPunct(v) {
continue // skip
} else if unicode.IsSpace(v) {
continue // skip
} else {
def = false
break
}
}
fmt.Println(input, def)
}
func IsNumeric(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}
Output:
123 true
123 true
12a3 false
12a3 false
abcd false
abcd false
12 3 true
12 3 false
12 3 45 6 7 true
12 3 45 6 7 false
12 3 a 45 6 7 false
12 3 a 45 6 7 false
12.3 true
12.3 true
-123.4 true
-123.4 true
References :
https://www.socketloop.com/tutorials/golang-natural-string-sorting-example
https://www.socketloop.com/tutorials/golang-how-to-check-if-input-from-os-args-is-integer
See also : Golang : Number guessing game with user input verification example
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.1k CloudFlare : Another way to get visitor's real IP address
+7.2k Golang : Of hash table and hash map
+29.2k Golang : Save map/struct to JSON or XML file
+14.7k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+20.1k Golang : How to get struct tag and use field name to retrieve data?
+15.8k Golang : Read a file line by line
+11.8k Golang : Convert(cast) bigint to string
+8.5k Golang : Convert(cast) []byte to io.Reader type
+14.1k Golang : syscall.Socket example
+10.5k Golang : Bubble sort example
+32.1k Golang : Convert []string to []byte examples
+44.7k Golang : Use wildcard patterns with filepath.Glob() example