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