Confusing ToUpper and ToTitle

See this example on the difference of titlecase/uppercase:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "dz"
    fmt.Println(strings.ToTitle(str))
    fmt.Println(strings.ToUpper(str))
}

(Note "dz" here is a single character, not a "d" followed by a "z": dz vs dz)

http://play.golang.org/p/xpDPLqKM9C


For a truly title case converting function, you must use -

strings.Title(strings.ToLower(str))

I tried the answers for converting a string to title case and none of the following works in case of a word which already has all uppercase characters or text that has few letters in uppercase and few in lowercase.

Here's a comprehensive check on what does what - http://ideone.com/r7nVbZ

I'm pasting the results here -

Given Text:  title case
ToTitle:  TITLE CASE
ToUpper:  TITLE CASE
Title:  Title Case
ToLower then Title:  Title Case
-------------------------------

Given Text:  Title Case
ToTitle:  TITLE CASE
ToUpper:  TITLE CASE
Title:  Title Case
ToLower then Title:  Title Case
-------------------------------

Given Text:  TITLE CASE
ToTitle:  TITLE CASE
ToUpper:  TITLE CASE
Title:  TITLE CASE
ToLower then Title:  Title Case
-------------------------------

Given Text:  TiTlE CasE
ToTitle:  TITLE CASE
ToUpper:  TITLE CASE
Title:  TiTlE CasE
ToLower then Title:  Title Case
-------------------------------

Given Text:  Title case
ToTitle:  TITLE CASE
ToUpper:  TITLE CASE
Title:  Title Case
ToLower then Title:  Title Case
-------------------------------

Given Text:  title CASE
ToTitle:  TITLE CASE
ToUpper:  TITLE CASE
Title:  Title CASE
ToLower then Title:  Title Case

I had the same problem. You want to use the strings.Title() method not the strings.ToTitle() method.

http://golang.org/pkg/strings/#Title

Tags:

Unicode

Go