混淆了ToUpper和ToTitle

huangapple go评论131阅读模式
英文:

Confusing ToUpper and ToTitle

问题

ToUpper函数和ToTitle函数之间有什么区别吗?

英文:

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

答案1

得分: 14

看一下标题大小写的区别的例子:

package main

import (
	"fmt"
	"strings"
)

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

(注意这里的 "dz" 是一个单个字符,不是 "d" 后面跟着 "z":dz vs dz)

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

英文:

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

答案2

得分: 13

我遇到了同样的问题。你应该使用strings.Title()方法而不是strings.ToTitle()方法。

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

英文:

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

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

答案3

得分: 7

对于一个真正的标题大小写转换函数,你必须使用以下代码:

strings.Title(strings.ToLower(str))

我尝试了将字符串转换为标题大小写的方法,但在已经包含所有大写字符的单词或者部分字母大写部分字母小写的文本的情况下,以下方法都不起作用。

这里是一个详细的检查,说明了每种方法的作用- http://ideone.com/r7nVbZ

我在这里粘贴了结果-

给定文本:title case
ToTitle: TITLE CASE
ToUpper: TITLE CASE
Title: Title Case
ToLower then Title: Title Case
-------------------------------

给定文本:Title Case
ToTitle: TITLE CASE
ToUpper: TITLE CASE
Title: Title Case
ToLower then Title: Title Case
-------------------------------

给定文本:TITLE CASE
ToTitle: TITLE CASE
ToUpper: TITLE CASE
Title: TITLE CASE
ToLower then Title: Title Case
-------------------------------

给定文本:TiTlE CasE
ToTitle: TITLE CASE
ToUpper: TITLE CASE
Title: TiTlE CasE
ToLower then Title: Title Case
-------------------------------

给定文本:Title case
ToTitle: TITLE CASE
ToUpper: TITLE CASE
Title: Title Case
ToLower then Title: Title Case
-------------------------------

给定文本:title CASE
ToTitle: TITLE CASE
ToUpper: TITLE CASE
Title: Title CASE
ToLower then Title: Title Case
英文:

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

答案4

得分: 5

strings.Title 函数已被弃用。现在以下是正确的做法:

package main

import (
	"fmt"

	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

func main() {
	caser := cases.Title(language.English)
	fmt.Println(caser.String("the quick brown fox jumps over the lazy dog"))
}

结果:

The Quick Brown Fox Jumps Over The Lazy Dog

程序已退出。

链接:https://go.dev/play/p/l1uCxNEiTPT

英文:

The strings.Title function is deprecated. The following is now the correct way to do this:

package main

import (
	"fmt"

	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

func main() {
	caser := cases.Title(language.English)
	fmt.Println(caser.String("the quick brown fox jumps over the lazy dog"))
}

Result:

The Quick Brown Fox Jumps Over The Lazy Dog

Program exited.

https://go.dev/play/p/l1uCxNEiTPT

答案5

得分: 3

根据unicode.org的说明:

"由于为了兼容性而包含了某些复合字符,例如U+01F1拉丁大写字母dz,因此引入了第三种情况,称为标题大小写,其中单词的第一个字符必须大写。一个例子是U+01F2拉丁大写字母d与小写字母z。这三种情况形式分别是大写、标题大小写和小写。"

这意味着当你对像dz这样的字符使用ToTitleToUpper时,你可能无法在视觉上区分结果,但这两种方法将返回不同的Unicode代码点。

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

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

英文:

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

答案6

得分: 2

尽管你在评论中说“源代码是相同的”,但实际情况并非如此(请参见L255和L277)。因此,这两个函数执行不同的任务,正如文档中所述。有关“大写”和“标题大小写”的定义,请参阅unicode.org上的文档。

英文:

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.

huangapple
  • 本文由 发表于 2013年9月1日 22:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/18559587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定