Confusing ToTitle and Title func names

657 views
Skip to first unread message

Yongjian Xu

unread,
Mar 27, 2012, 2:01:00 AM3/27/12
to golan...@googlegroups.com
In strings package, ToTitle() and Title() are doing something different but they have very closed and confused names. And ToTitle() currently seems to do exactly the same thing as what ToUpper() does. So if I really want title words, I should use just Title(). So why we leave ToTitle() there? Maybe I have missed something.


peterGo

unread,
Mar 27, 2012, 3:08:58 AM3/27/12
to golang-nuts
Yongjian Xu,

Q: What is titlecase? How is it different from uppercase?

http://unicode.org/faq/casemap_charprop.html#4

Peter

peterGo

unread,
Mar 27, 2012, 3:13:16 AM3/27/12
to golang-nuts
Yongjian Xu,

If you want title words, use strings.Title().

Title returns a copy of the string s with all Unicode letters that
begin words mapped to their title case.
http://weekly.golang.org/pkg/strings/#Title

Peter

On Mar 27, 2:01 am, Yongjian Xu <ji...@google.com> wrote:

Yongjian Xu

unread,
Mar 27, 2012, 3:22:16 AM3/27/12
to golan...@googlegroups.com

func main() {
    log.Printf("==Title: %s",
               strings.Title("abc bcd"))
    log.Printf("==ToTitle: %s",
               strings.ToTitle("abc bcd"))
    log.Printf("==ToUpper: %s",
               strings.ToUpper("abc bcd"))
}

2012/03/26 22:52:46 ==Title: Abc Bcd
2012/03/26 22:52:46 ==ToTitle: ABC BCD
2012/03/26 22:52:46 ==ToUpper: ABC BCD

Only Title() works as what the function name suggests. ToTitle() just returns everything Upper. What did I miss here?

Peter Bourgon

unread,
Mar 27, 2012, 3:25:56 AM3/27/12
to Yongjian Xu, golan...@googlegroups.com
> Only Title() works as what the function name suggests. ToTitle() just
> returns everything Upper. What did I miss here?

The Title case form of a character is, in many languages, different
than Upper case form of that character. (English is not one of them,
as your example demonstrates.) That's the distinction.

I agree that there is some confusion in ToTitle and Title performing
different work.

Yongjian Xu

unread,
Mar 27, 2012, 3:32:41 AM3/27/12
to golan...@googlegroups.com, Yongjian Xu


On Tuesday, March 27, 2012 12:25:56 AM UTC-7, Peter Bourgon wrote:
> Only Title() works as what the function name suggests. ToTitle() just
> returns everything Upper. What did I miss here?

The Title case form of a character is, in many languages, different
than Upper case form of that character. (English is not one of them,
as your example demonstrates.) That's the distinction.

I agree that there is some confusion in ToTitle and Title performing
different work.

Ah... ok. I missed the i18n stuff. Yes, it probably deserve a distinguishable name from Title() then.
 

peterGo

unread,
Mar 27, 2012, 3:34:03 AM3/27/12
to golang-nuts
Yongjian Xu,

Go applies the Unicode rules.

3.13 Default Case Algorithms
http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf#G33992

SpecialCasing.txt plus the mappings from UnicodeData.txt,
http://www.unicode.org/versions/Unicode6.0.0/ch04.pdf#G124722

In your example, the Unicode codepoints are both upper and title case.

Peter
Reply all
Reply to author
Forward
0 new messages