Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 332
jersey bean Avatar asked Dec 11 '19 20:12

jersey bean


People also ask

How do you check if a string is lower case in Golang?

You are allowed to check the given rune is a lower case letter or not with the help of IsLower() function. This function returns true if the given rune is a lower case letter, or return false if the given rune is not a lower case letter.

How do you know if a string is upper or lower case?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!

How do you check if all strings are uppercase?

To check if a string is in uppercase, we can use the isupper() method. isupper() checks whether every case-based character in a string is in uppercase, and returns a True or False value depending on the outcome.

Is upper case Golang?

Golang String to Uppercase To convert string to upper case in Go programming, call strings. ToUpper() function and pass the string as argument to this function.


2 Answers

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
}
like image 194
JimB Avatar answered Oct 09 '22 19:10

JimB


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)
}
like image 13
jersey bean Avatar answered Oct 09 '22 18:10

jersey bean