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.6k Javascript : How to check a browser's Do Not Track status?
+6.9k Golang : Dealing with postal or zip code example
+4.4k MariaDB/MySQL : How to get version information
+12.2k Golang : "https://" not allowed in import path
+28.2k Get file path of temporary file in Go
+13.5k Golang : How to determine if a year is leap year?
+6.9k Golang : Get Alexa ranking data example
+8.6k Golang : Gaussian blur on image and camera video feed examples
+11k Golang : How to pipe input data to executing child process?
+3.8k Detect if Google Analytics and Developer Media are loaded properly or not
+19.1k Golang : Fix cannot download, $GOPATH not set error