Go验证字符串中是否包含中文(推荐)-创新互联
发现一个验证字符串是否包含中文滴时候,一个比正则更好使滴方法,而且是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
}另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻标题:Go验证字符串中是否包含中文(推荐)-创新互联
分享网址:http://www.jxjierui.cn/article/ceipih.html


咨询
建站咨询
