How to check if a string is all upper or lower case in Go?

13,528

Solution 1

You can of course compare the upper and lower cased strings in their entirety, or you can short-circuit the comparisons on the first failure, which would be more efficient when comparing long strings.

func IsUpper(s string) bool {
    for _, r := range s {
        if !unicode.IsUpper(r) && unicode.IsLetter(r) {
            return false
        }
    }
    return true
}

func IsLower(s string) bool {
    for _, r := range s {
        if !unicode.IsLower(r) && unicode.IsLetter(r) {
            return false
        }
    }
    return true
}

Solution 2

One solution is to use strings.ToUpper()/ToLower() and compare with the original string. This works for the punctuation case as well.

Here's the solution:

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "UPPERCASE"
    fmt.Println(strings.ToUpper(s) == s)

    s = "lowercase"
    fmt.Println(strings.ToUpper(s) == s)

    s = "lowercase"
    fmt.Println(strings.ToLower(s) == s)

    s = "I'M YELLING AT YOU!"
    fmt.Println(strings.ToUpper(s) == s)
}
Share:
13,528
jersey bean
Author by

jersey bean

Software engineer with over 20+ years of industry experience. I've worked mostly in the data storage industry, but I would consider myself a generalist. As of recent I code mostly in Golang and Python in a micro services (containers) environment. Although I have experience in several other languages such as C, C++, Java, etc... I have a passion for algorithmic and quantitative trading; I built my own trading platform in Python, ReactJS/NodeJS, and Mongodb.

Updated on June 11, 2022

Comments

  • jersey bean
    jersey bean almost 2 years

    What is an easy way in Golang to check if all characters in a string are upper case or lower case?

    Also, how to handle a case where the string has punctuation?

    See these examples:

    package main
    
    import (
        "fmt"
        "unicode"
    )
    
    func main() {
        s := "UPPERCASE"
        fmt.Println(s.IsUpper())  // Should print true
    
        s = "lowercase"
        fmt.Println(s.IsUpper())  // Should print false
    
        s = "lowercase"
        fmt.Println(s.IsLower())  // Should print true
    
        s = "I'M YELLING AT YOU!"
        fmt.Println(s.IsUpper())  // Should print true
    }
    

    Note: s.IsUpper() and s.IsLower() doesn't really exist, but would be nice to find an equivalent.

  • jersey bean
    jersey bean over 4 years
    So I guess I'll state the obvious. I think your point is that using strings.ToUpper is a O(n) operation and is costly for really long strings (as n grows large). So your solution uses unicode.IsUpper and will short circuit as soon as your find a character which is not upper case. Good answer!
  • jersey bean
    jersey bean over 4 years
    See my comment to @JimB solution. Its more efficient than this solution.
  • Carson
    Carson almost 2 years
    each has his strong point. consider the case 中文a1