How to check lowercase characters in a string in Golang?
Checking if all characters of a string are lowercase evaluates to True or False. For example, checking if the string "test" is lowercase evaluates to True.
Use strings.ToLower(s) to check are characters are in lowercase
package main
import (
"fmt"
"strings"
)
func main() {
s := "lowercase"
fmt.Println(strings.ToLower(s) == s) // true
s = "can't say"
fmt.Println(strings.ToLower(s) == s) // true
}
Use unicode.IsLower(s) to verify all characters are in lowercase
package main
import (
"fmt"
"unicode"
)
func IsLower(s string) bool {
for _, r := range s {
if !unicode.IsLower(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
func main() {
fmt.Println(IsLower("lowercase")) // true
fmt.Println(IsLower("can't")) // true
}
Most Helpful This Week
How to count number of repeating words in a given String?
Constructors in Golang
Golang Read Write and Process data in CSV
Regular expression to extract all Non-Alphanumeric Characters from a String
How to create Empty and Nil Slice?
Simple example of Map initialization in Go
How to use function from another file golang?
User Defined Function Types in Golang
How to Convert string to float type in Go?
Golang download image from given URL