Golang 检查Rune是否是Unicode标点符号字符
符文是ASCII的一个超集,或者说是int32的一个别名。它容纳了世界上所有的书写系统中的字符,包括重音和其他变音符号,控制代码,如TAB和回车,并给每个字符分配一个标准的数字。这个标准数字在Go语言中被称为Unicode码位或符文。
在 IsPunct() 函数的帮助下,你可以检查给定的符文是否为Unicode标点符号。如果给定的符文是Unicode标点符号,该函数返回true;如果给定的符文不是Unicode标点符号,则返回false。这个函数是在Unicode包中定义的,所以要使用这个方法,你需要在你的程序中导入Unicode包。
语法
func IsPunct(r rune) bool
这个函数的返回类型是布尔型。让我们借助给定的例子来讨论这个概念。
例子
// Go program to illustrate how to check the
// given rune is a Unicode punctuation
// character or not
package main
import (
"fmt"
"unicode"
)
// Main function
func main() {
// Creating rune
rune_1 := 'g'
rune_2 := 'e'
rune_3 := '!'
rune_4 := ','
rune_5 := 'S'
// Checking the given rune is a Unicode
// punctuation character or not
// Using IsPunct() function
res_1 := unicode.IsPunct(rune_1)
res_2 := unicode.IsPunct(rune_2)
res_3 := unicode.IsPunct(rune_3)
res_4 := unicode.IsPunct(rune_4)
res_5 := unicode.IsPunct(rune_5)
// Displaying results
fmt.Println(res_1)
fmt.Println(res_2)
fmt.Println(res_3)
fmt.Println(res_4)
fmt.Println(res_5)
}
输出
false
false
true
true
false
例2 :
// Go program to illustrate how to check the
// given rune is a Unicode punctuation
// character or not
package main
import (
"fmt"
"unicode"
)
// Main function
func main() {
// Creating a slice of rune
val := []rune{'g', 'f', 'G', '#', ',', ':'}
// Checking each element of the given slice
// of the rune is a Unicode punctuation
// character or not
// Using IsPunct() function
for i := 0; i < len(val); i++ {
if unicode.IsPunct(val[i]) == true {
fmt.Println("It is a Unicode punctuation character")
} else {
fmt.Println("It is not a Unicode punctuation character")
}
}
}
输出
It is not a Unicode punctuation character
It is not a Unicode punctuation character
It is not a Unicode punctuation character
It is a Unicode punctuation character
It is a Unicode punctuation character
It is a Unicode punctuation character