Golang strconv.IsGraphic
最后修改于 2025 年 4 月 20 日
本教程解释了如何在 Go 中使用 strconv.IsGraphic 函数。我们将通过实际示例涵盖 Unicode 可打印字符的检测。
strconv.IsGraphic 函数报告一个 rune 是否为 Unicode 定义的“可打印”字符。可打印字符包括字母、标记、数字、标点符号、符号和空格。
可打印字符是任何打算书写、打印或显示的字符。这排除了控制字符和其他非打印字符。
基本的 strconv.IsGraphic 示例
strconv.IsGraphic 最简单的用法是检查一个 rune 是否是可打印的。这里我们测试几个常用字符。
package main
import (
"fmt"
"strconv"
)
func main() {
chars := []rune{'A', ' ', '\n', '€', '\t'}
for _, c := range chars {
if strconv.IsGraphic(c) {
fmt.Printf("%c is a graphic character\n", c)
} else {
fmt.Printf("%c is not a graphic character\n", c)
}
}
}
我们测试字母、空格、换行符和特殊字符。该函数对可见字符和空格返回 true,对控制字符返回 false。
测试 Unicode 字符
strconv.IsGraphic 支持完整的 Unicode 范围。此示例测试了来自不同脚本的各种 Unicode 字符。
package main
import (
"fmt"
"strconv"
)
func main() {
tests := []rune{
'A', // Latin
'あ', // Hiragana
'汉', // Han
'☺', // Emoji
'\u200b', // Zero-width space
'\u00ad', // Soft hyphen
}
for _, r := range tests {
fmt.Printf("%U %c: %t\n", r, r, strconv.IsGraphic(r))
}
}
我们测试了来自不同脚本的字符和特殊情况。该函数正确识别了可见字符并排除了格式控制字符。
与 IsPrint 比较
IsGraphic 与 unicode.IsPrint 类似,但在处理空格方面有所不同。此示例显示了这些差异。
package main
import (
"fmt"
"strconv"
"unicode"
)
func main() {
chars := []rune{' ', '\t', '\n', 'A', '1'}
for _, c := range chars {
fmt.Printf("%c: IsGraphic=%t, IsPrint=%t\n",
c, strconv.IsGraphic(c), unicode.IsPrint(c))
}
}
IsGraphic 将空格视为可打印字符,而 IsPrint 不将其视为可打印字符。两者在其他可打印字符和控制码上意见一致。
检查字符串字符
此示例演示了检查字符串中的每个字符是否为可打印字符。这对于输入验证很有用。
package main
import (
"fmt"
"strconv"
)
func main() {
input := "Hello\t世界\n"
for i, r := range input {
if strconv.IsGraphic(r) {
fmt.Printf("Character %d '%c' is graphic\n", i, r)
} else {
fmt.Printf("Character %d (U+%04X) is not graphic\n", i, r)
}
}
}
我们遍历字符串中的每个 rune 并检查其可打印状态。输出显示了哪些字符被函数视为可打印字符。
输入验证示例
一个实际的应用场景是验证用户输入是否只包含可打印字符。此示例展示了如何实现此类验证。
package main
import (
"fmt"
"strconv"
)
func validateInput(input string) bool {
for _, r := range input {
if !strconv.IsGraphic(r) {
return false
}
}
return true
}
func main() {
tests := []string{"Hello 世界", "Text with\ttab", "Bad\u0001Input"}
for _, s := range tests {
if validateInput(s) {
fmt.Printf("'%s' is valid\n", s)
} else {
fmt.Printf("'%s' contains non-graphic characters\n", s)
}
}
}
validateInput 函数会检查字符串中的每个 rune。它会拒绝包含任何非可打印字符(如控制码)的字符串。
处理特殊情况
某些 Unicode 字符具有特殊处理。此示例探讨了边缘情况和特殊字符。
package main
import (
"fmt"
"strconv"
)
func main() {
specialChars := []rune{
'\u00a0', // Non-breaking space
'\u200b', // Zero-width space
'\u00ad', // Soft hyphen
'\u2028', // Line separator
'\u2029', // Paragraph separator
}
for _, c := range specialChars {
fmt.Printf("%U: IsGraphic=%t\n", c, strconv.IsGraphic(c))
}
}
我们测试了各种特殊的空格和分隔符字符。该函数在处理这些边缘情况时的行为遵循 Unicode 标准对可打印字符的定义。
性能基准测试
对于性能敏感的代码,了解 IsGraphic 的开销很有帮助。此示例对其与替代方案进行了基准测试。
package main
import (
"fmt"
"strconv"
"testing"
"unicode"
)
func BenchmarkIsGraphic(b *testing.B) {
for i := 0; i < b.N; i++ {
strconv.IsGraphic('A')
}
}
func BenchmarkIsPrint(b *testing.B) {
for i := 0; i < b.N; i++ {
unicode.IsPrint('A')
}
}
func main() {
fmt.Println("Run benchmarks with: go test -bench=.")
}
IsGraphic 针对性能进行了优化,但比 unicode.IsPrint 稍慢。对于大多数用例,差异可以忽略不计。
来源
本教程通过在各种场景下对 Unicode 可打印字符进行检测的实际示例,介绍了 Go 中的 strconv.IsGraphic 函数。