Confusing ToUpper and ToTitle

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

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

5条回答
  •  心在旅途
    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
    

提交回复
热议问题