dptn69182
2016-07-24 16:40
浏览 2.1k
已采纳

如何在Go中检查字符串是否仅包含字母字符?

I am trying to check the username whether is only containing alphabetic characters. What is the idiomatic way to check it in Go?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试检查用户名是否仅包含字母字符。 在Go中检查它的惯用方式是什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongpangbu4016 2016-07-24 16:55
    已采纳

    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
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongtai6741 2016-07-24 16:59

    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
    }
    
    评论
    解决 无用
    打赏 举报
  • douxiao0400 2016-07-24 17:34

    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
    ", username)
        }
    }
    

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

    评论
    解决 无用
    打赏 举报
  • dpfln86244 2019-06-18 09:29

    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
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题