How to check if a string is title case in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
    "unicode"
)

func IsTitleCase(s string) bool {
    words := strings.Split(s, " ")
    for _, w := range words {
        if w == "" {
            continue
        }
        runes := []rune(w)
        if !unicode.IsUpper(runes[0]) {
            return false
        }
        for _, r := range runes[1:] {
            if !unicode.IsLower(r) {
                return false
            }
        }
    }
    return true
}

func main() {
    fmt.Println(IsTitleCase("Hello World")) // true
    fmt.Println(IsTitleCase("Hello world")) // false
}



/*
run:

true
false

*/

 



answered 2 hours ago by avibootz
...