you can use unicode.IsLetteras this working example code:
package main
import "fmt"
import "unicode"
func IsLetter(s string) bool {
for _, r := range s {
if !unicode.IsLetter(r) {
return false
}
}
return true
}
func main() {
fmt.Println(IsLetter("Alex"))
fmt.Println(IsLetter("123"))
}
or if you have a limited range, for example. 'a' .. 'z' and 'A' .. 'Z', you can use this working code example:
package main
import "fmt"
func IsLetter(s string) bool {
for _, r := range s {
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
return false
}
}
return true
}
func main() {
fmt.Println(IsLetter("Alex"))
fmt.Println(IsLetter("123 a"))
}
or if you have a limited range, for example. 'a' .. 'z' and 'A' .. 'Z', you can use this working code example:
package main
import "fmt"
import "regexp"
var IsLetter = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
func main() {
fmt.Println(IsLetter("Alex"))
fmt.Println(IsLetter("u123"))
}
user6169399
source
share