Golang 如何把符文映射到标题大小写
Rune是ASCII的一个超集,或者说是int32的一个别名。它容纳了世界上所有可用的书写系统的字符,包括重音和其他变音符号,控制代码,如制表符和回车符,并为每个字符分配了一个标准号码。这个标准数字在Go语言中被称为Unicode码位或符文。
在 ToTitle() 函数的帮助下,你可以将给定的符文映射到标题大小写。这个函数将给定符文的大小写(如果符文的大小写是小写或大写)改为标题大小写,如果给定的符文已经存在于标题大小写中,那么这个函数不做任何事情。这个函数是在Unicode包中定义的,所以要使用这个方法,你需要在你的程序中导入Unicode包。
语法
func ToTitle(r rune) rune
例1 :
// Go program to illustrate how to
// map a rune to title case
package main
import (
"fmt"
"unicode"
)
// Main function
func main() {
// Creating rune
rune_1 := 'g'
rune_2 := 'e'
rune_3 := 'E'
rune_4 := 'k'
// Mapping the given rune into title case
// Using ToTitle() function
fmt.Printf("Result 1: %c ", unicode.ToTitle(rune_1))
fmt.Printf("\nResult 2: %c ", unicode.ToTitle(rune_2))
fmt.Printf("\nResult 3: %c ", unicode.ToTitle(rune_3))
fmt.Printf("\nResult 4: %c ", unicode.ToTitle(rune_4))
fmt.Printf("\nResult 5: %c ", unicode.ToTitle('s'))
}
输出
Result 1: G
Result 2: E
Result 3: E
Result 4: K
Result 5: S
例2 :
// Go program to illustrate how to
// map a rune to title case
package main
import (
"fmt"
"unicode"
)
// Main function
func main() {
// Creating rune
rune_1 := 'r'
rune_2 := 'U'
rune_3 := 'n'
rune_4 := 'E'
// Mapping the given rune into title case
// Using ToTitle() function
fmt.Printf("Result 1: %c ", unicode.ToTitle(rune_1))
fmt.Printf("\nResult 2: %c ", unicode.ToTitle(rune_2))
fmt.Printf("\nResult 3: %c ", unicode.ToTitle(rune_3))
fmt.Printf("\nResult 4: %c ", unicode.ToTitle(rune_4))
}
输出
Result 1: R
Result 2: U
Result 3: N
Result 4: E
极客教程