猿问

检查一个字符串只包含 ASCII 字符

Go 是否有任何方法或建议如何检查字符串是否仅包含 ASCII 字符?正确的做法是什么?


根据我的研究,解决方案之一是检查任何大于 127 的字符。


func isASCII(s string) bool {

    for _, c := range s {

        if c > unicode.MaxASCII {

            return false

        }

    }


    return true

}


哔哔one
浏览 139回答 3
3回答

慕桂英546537

在 Go 中,我们关心性能,因此,我们会对您的代码进行基准测试:func isASCII(s string) bool {&nbsp; &nbsp; for _, c := range s {&nbsp; &nbsp; &nbsp; &nbsp; if c > unicode.MaxASCII {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}BenchmarkRange-4&nbsp; &nbsp; 20000000&nbsp; &nbsp; 82.0 ns/op一个更快(更好,更惯用)的版本,它避免了不必要的符文转换:func isASCII(s string) bool {&nbsp; &nbsp; for i := 0; i < len(s); i++ {&nbsp; &nbsp; &nbsp; &nbsp; if s[i] > unicode.MaxASCII {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}BenchmarkIndex-4&nbsp; &nbsp; 30000000&nbsp; &nbsp; 55.4 ns/opascii_test.go:package mainimport (&nbsp; &nbsp; "testing"&nbsp; &nbsp; "unicode")func isASCIIRange(s string) bool {&nbsp; &nbsp; for _, c := range s {&nbsp; &nbsp; &nbsp; &nbsp; if c > unicode.MaxASCII {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}func BenchmarkRange(b *testing.B) {&nbsp; &nbsp; str := ascii()&nbsp; &nbsp; b.ResetTimer()&nbsp; &nbsp; for N := 0; N < b.N; N++ {&nbsp; &nbsp; &nbsp; &nbsp; is := isASCIIRange(str)&nbsp; &nbsp; &nbsp; &nbsp; if !is {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.Fatal("notASCII")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func isASCIIIndex(s string) bool {&nbsp; &nbsp; for i := 0; i < len(s); i++ {&nbsp; &nbsp; &nbsp; &nbsp; if s[i] > unicode.MaxASCII {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}func BenchmarkIndex(b *testing.B) {&nbsp; &nbsp; str := ascii()&nbsp; &nbsp; b.ResetTimer()&nbsp; &nbsp; for N := 0; N < b.N; N++ {&nbsp; &nbsp; &nbsp; &nbsp; is := isASCIIIndex(str)&nbsp; &nbsp; &nbsp; &nbsp; if !is {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.Log("notASCII")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func ascii() string {&nbsp; &nbsp; byt := make([]byte, unicode.MaxASCII+1)&nbsp; &nbsp; for i := range byt {&nbsp; &nbsp; &nbsp; &nbsp; byt[i] = byte(i)&nbsp; &nbsp; }&nbsp; &nbsp; return string(byt)}输出:$ go test ascii_test.go -bench=.BenchmarkRange-4&nbsp; &nbsp; 20000000&nbsp; &nbsp; 82.0 ns/opBenchmarkIndex-4&nbsp; &nbsp; 30000000&nbsp; &nbsp; 55.4 ns/op$

心有法竹

看起来你的方法是最好的。ASCII简单定义为:ASCII 将 128 个指定字符编码为七位整数因此,字符的值为 0-2 7(或 0-127、0x0-0x7F)。Go 无法检查字符串中的每个符文(或切片中的字节)是否具有特定范围内的数值,因此您的代码似乎是最好的方法。

撒科打诨

另外一个选择:package mainimport "golang.org/x/exp/utf8string"func main() {&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; b := utf8string.NewString("south north").IsASCII()&nbsp; &nbsp; &nbsp; println(b) // true&nbsp; &nbsp;}&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; b := utf8string.NewString("🧡💛💚💙💜").IsASCII()&nbsp; &nbsp; &nbsp; println(b) // false&nbsp; &nbsp;}}https://pkg.go.dev/golang.org/x/exp/utf8string#String.IsASCII
随时随地看视频慕课网APP

相关分类

Go
我要回答