Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string only contains alphabetic characters in Go?

Tags:

go

People also ask

How do you check if a string contains only the alphabet?

We can use the regex ^[a-zA-Z]*$ to check a string for alphabets. This can be done using the matches() method of the String class, which tells whether the string matches the given regex.

How do you check if a string is alphanumeric in Go?

Using the MustCompiler function, we can check if the regular expression is satisfied or not, We have parsed the string ^[a-zA-Z0-9_]*$ which will check from start(^) to end($) any occurrences of the characters from 0-9, A-Z and a-z.

How do you make sure a string only has letters?

In order to check if a String has only Unicode letters in Java, we use the isDigit() and charAt() methods with decision-making statements. The isLetter(int codePoint) method determines whether the specific character (Unicode codePoint) is a letter. It returns a boolean value, either true or false.


you may use unicode.IsLetter like this working sample 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")) // true
    fmt.Println(IsLetter("123"))  // false
}

or if you have limited range e.g. 'a'..'z' and 'A'..'Z', you may use this working sample code:

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"))  // true
    fmt.Println(IsLetter("123 a")) // false

}

or if you have limited range e.g. 'a'..'z' and 'A'..'Z', you may use this working sample code:

package main

import "fmt"
import "regexp"

var IsLetter = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString

func main() {
    fmt.Println(IsLetter("Alex")) // true
    fmt.Println(IsLetter("u123")) // false
}

Assuming you're only looking for ascii letters, you would normally see this implemented as a regular expression using the alpha character class [[:alpha:]] or the equivalent [A-Za-z]

isAlpha := regexp.MustCompile(`^[A-Za-z]+$`).MatchString

for _, username := range []string{"userone", "user2", "user-three"} {
    if !isAlpha(username) {
        fmt.Printf("%q is not valid\n", username)
    }
}

https://play.golang.org/p/lT9Fki7tt7


Here's the way I'd do it:

import "strings"

const alpha = "abcdefghijklmnopqrstuvwxyz"

func alphaOnly(s string) bool {
   for _, char := range s {  
      if !strings.Contains(alpha, strings.ToLower(string(char))) {
         return false
      }
   }
   return true
}

You can also do this concisely without importing any packages

func isLetter(c rune) bool {
    return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
}

func isWord(s string) bool {
    for _, c := range s {
        if !isLetter(c) {
            return false
        }
    }
    return true
}