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
Regex to extract image name from HTML in Golang
Regular expression to extract all Non-Alphanumeric Characters from a String
Copy an array by value and reference into another array
Get current date and time in various format in golang
Split a string at uppercase letters using regular expression in Golang
How to add Watermark or Merge two image?
How to kill execution of goroutine?
How to delete or remove element from a Map?
Various examples of Carbon date-time package in Golang
How can we reverse a simple string in Go?