Confusing ToUpper and ToTitle

后端 未结 5 1283
长发绾君心
长发绾君心 2021-01-01 16:18

Is there any difference between the ToUpper and the ToTitle function?

相关标签:
5条回答
  • 2021-01-01 16:38

    According to unicode.org

    "Because of the inclusion of certain composite characters for compatibility, such as U+01F1 latin capital letter dz, a third case, called titlecase, is used where the first character of a word must be capitalized. An example of such a character is U+01F2 latin capital letter d with small letter z. The three case forms are UPPERCASE, Titlecase, and lowercase."

    This means that when you use ToTitle or ToUpper for characters like dz, you'll probably not be able to visually distinguish the result, but the two methods will return different unicode code points.

    dz           = "\u01f3"
    ToUpper(dz)  = "\u01f1"
    ToTittle(dz) = "\u01f2"
    

    https://play.golang.org/p/OAjONd87y2

    0 讨论(0)
  • 2021-01-01 16:43

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

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

    0 讨论(0)
  • 2021-01-01 16:43

    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
    
    0 讨论(0)
  • 2021-01-01 16:51

    Even though you're saying in your comment that "The source codes are the same." it's actually not the case (see L255 vs L277). Therefore those two function perform different tasks, exactly as documented. For the definition of "upper case" and "title case" please see the documentation at unicode.org.

    0 讨论(0)
  • 2021-01-01 16:54

    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

    0 讨论(0)
提交回复
热议问题