发现一个验证字符串是否包含中文滴时候,一个比正则更好使滴方法,而且是golang 自带滴验证。
不需要自己写正则验证,代码如下:
package main
import (
"fmt"
"regexp"
"unicode"
)
func main() {
s1 := "我是中国人hello word!,2020 street 188#"
var count int
for _, v := range s1 {
if unicode.Is(unicode.Han, v) {
fmt.Println("找到中文")
count++
}
}
fmt.Println(count)
fmt.Println(IsChineseChar(s1))
}
// 或者封装函数调用
func IsChineseChar(str string) bool {
for _, r := range str {
if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[u3002uff1buff0cuff1au201cu201duff08uff09u3001uff1fu300au300b]").MatchString(string(r))) {
return true
}
}
return false
}
比正则好用