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