Check if string is int
GoGo Problem Overview
How can I check if a string value is an integer or not in Go?
Something like
v := "4"
if isInt(v) {
fmt.Println("We have an int, we can safely cast this with strconv")
}
Note: I know that strconv.Atoi
returns an error, but is there any other function to do this?
The problem with strconv.Atoi
is that it will return 7
for "a7"
Go Solutions
Solution 1 - Go
As you said, you can use strconv.Atoi for this.
if _, err := strconv.Atoi(v); err == nil {
fmt.Printf("%q looks like a number.\n", v)
}
You could use scanner.Scanner
(from text/scanner
) in mode ScanInts
, or use a regexp to validate the string, but Atoi
is the right tool for the job.
Solution 2 - Go
this is better, you can check for ints upto 64 ( or less ) bits
strconv.Atoi only supports 32 bits
if _, err := strconv.ParseInt(v,10,64); err == nil {
fmt.Printf("%q looks like a number.\n", v)
}
try it out with v := "12345678900123456789"
Solution 3 - Go
You can use unicode.IsDigit()
:
import "unicode"
func isInt(s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
Solution 4 - Go
You can use govalidator
.
Code
govalidator.IsInt("123") // true
Full Example
package main
import (
"fmt"
valid "github.com/asaskevich/govalidator"
)
func main() {
fmt.Println("Is it a Integer? ", valid.IsInt("978"))
}
Output:
$ go run intcheck.go
Is it a Integer? true
Solution 5 - Go
You might also use regexp to check this.
It can be a little overkill, but it also gives you more flexibility if you want to extend your rules.
Here some code example:
package main
import (
"regexp"
)
var digitCheck = regexp.MustCompile(`^[0-9]+$`)
func main() {
digitCheck.MatchString("1212")
}
If you want to see it running: https://play.golang.org/p/6JmzgUGYN3u
Hope it helps ;)
Solution 6 - Go
this might help
func IsInt(s string) bool {
l := len(s)
if strings.HasPrefix(s, "-") {
l = l - 1
s = s[1:]
}
reg := fmt.Sprintf("\\d{%d}", l)
rs, err := regexp.MatchString(reg, s)
if err != nil {
return false
}
return rs
}
Solution 7 - Go
This manually checks each CHAR to see if it falls between 0 and 9.
func IsDigitsOnly(s string) bool {
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}
Solution 8 - Go
you can check if all runes are between 0 and 9
isNotDigit := func(c rune) bool { return c < '0' || c > '9' }
b := strings.IndexFunc(s, isNotDigit) == -1
Solution 9 - Go
Most of the above posted solutions are neat and nice.
Below is a simple solution without using any library:
func IsNumericOnly(str string) bool {
if len(str) == 0 {
return false
}
for _, s := range str {
if s < '0' || s > '9' {
return false
}
}
return true
}
Test cases
func TestIsNumericOnly(t *testing.T) {
cases := []struct {
name string
str string
isNumeric bool
}{
{
name: "numeric string",
str: "0123456789",
isNumeric: true,
},
{
name: "not numeric string",
str: "#0123456789",
isNumeric: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
assert.Equal(t, c.isNumeric, IsNumericOnly(c.str))
})
}
}