发现一个验证字符串是否包含中文滴时候,一个比正则更好使滴方法,并且是golang 自带滴验证。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("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) { return true } } return false }
比正则好用spa