How to check UPPERCASE characters in a string in Golang?
Checking if all characters of a string are Uppercase evaluates to True or False. For example, checking if the string "TEST" is uppercase evaluates to True.
Use strings.ToUpper(s) to check are characters are in uppercase
package main
import (
"fmt"
"strings"
)
func main() {
s := "UPPERCASE"
fmt.Println(strings.ToUpper(s) == s) // true
s = "CAN'T"
fmt.Println(strings.ToUpper(s) == s) // true
}
Use unicode.IsUpper(s) to verify all characters are in uppercase
package main
import (
"fmt"
"unicode"
)
func IsUpper(s string) bool {
for _, r := range s {
if !unicode.IsUpper(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
func main() {
fmt.Println(IsUpper("UPPERCASE")) // true
fmt.Println(IsUpper("CAN'T")) // true
}
Most Helpful This Week
Data encryption with AES-GCM
Replace first occurrence of string using Regexp
How to check if a string contains only letters in Golang?
Regex to extract image name from HTML in Golang
Example of Pointers with Struct
How to create Empty and Nil Slice?
Create and Print Multi Dimensional Slice in Golang
Golang Get current Date and Time in EST, UTC and MST?
How to get the current date and time with timestamp in local and other timezones ?
Different ways to validate JSON string