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
+20.6k Golang : How to reverse slice or array elements order
+13.2k Golang : GUI with Qt and OpenCV to capture image from camera
+21.7k Golang : Randomly pick an item from a slice/array example
+7k Golang : Add build version and other information in executables
+17.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+5.5k Golang : Embedded or data bundling example
+6.5k Golang : What fmt.Println() can do and println() cannot do
+13.5k Golang : Execute function at intervals or after some delay
+14.4k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+46.2k Golang : How to convert JSON string to map and slice
+6.8k Javascript : How to check a browser's Do Not Track status?
+37.8k Golang : How to iterate over a []string(array)