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
+4.9k Golang : Check if a word is countable or not
+4.1k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+13.2k Golang : Get constant name from value
+6.9k Golang : Gorrila mux.Vars() function example
+14k Elastic Search : Mapping date format and sort by date
+37.7k Golang : Read a text file and replace certain words
+22.5k Golang : untar or extract tar ball archive example
+12.4k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+10.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+51.1k Golang : Check if item is in slice/array
+5.8k nginx : force all pages to be SSL