Unicode Package
unicode package提供的函数用于查找Unicode代码点是否符合特定的条件。 举例来说,每一个字符是否为数字或者小写字符。常用的unicode.Is()函数用于判断一个字符是某一类Unicode(16进制字符,大小,小写)
Table 3.11 The Unicode Package’s Functions
Syntax | Description/result |
---|---|
unicode.Is(table, c) | 如果字符c,在table中则返回true |
unicode.IsControl(c) | 如果字符c是控制字符,则返回true |
unicode.IsDigit(c) | c为数字字符 |
unicode.IsGraphic(c) | graphic字符包含字母,数字,标点符号,其它符号,空格 |
unicode.IsLetter(c) | true if c is a letter |
unicode.IsLower(c) | true if c is a lowercase letter |
unicode.IsMark(c) | true if c is a mark character |
unicode.IsOneOf(tables, c) | true if c is in one of the tables |
unicode.IsPrint(c) | true if c is a printable character |
unicode.IsPunct(c) | 标点符号 |
unicode.IsSpace(c) | true if c is a whitespace character |
unicode.IsSymbol(c) | true if c is a symbol character |
unicode.IsTitle(c) | true if c is a title-case letter |
unicode.IsUpper(c) | true if c is an uppercase letter |
unicode.SimpleFold(c) | A case-folded copy of the given c |
unicode.To(case, c) | The case version of c where case is unicode.LowerCase, unicode.TitleCase, or unicode.UpperCase |
unicode.ToLower(c) | The lowercase version of c |
unicode.ToTitle(c) | The title-case version of c |
unicode.ToUpper(c) | The uppercase version of c |
fmt.Println(IsHexDigit('8'), IsHexDigit('x'), IsHexDigit('X'),
IsHexDigit('b'), IsHexDigit('B'))
// true·false·false·true·true
unicode package 提供了unicode.IsDigit()函数用来检测一个字符是否为数字。但它不存在检测是否为16进制数字的方法,所以我们自定义了IsHexDigit()函数
func IsHexDigit(char rune) bool {
return unicode.Is(unicode.ASCII_Hex_Digit, char)
}
这个函数使用通用的unicode.Is(), 连同unicode.ASCII_HEX_DIGIT 范围以确定字符是否为一个16进制数字。我们可以创建其它类似的函数,用来测试Unicode 字符集。