From 42dea290393e20e876f7fb45b3fc8309d3ad3f12 Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Mon, 13 Sep 2021 23:53:07 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20unicode.Is=20=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/string/main.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/base/string/main.go b/base/string/main.go index d61a077..89521e0 100644 --- a/base/string/main.go +++ b/base/string/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "strings" + "unicode" ) func main() { @@ -41,6 +42,15 @@ func main() { fmt.Printf("b1: []byte: %+v \t string: %s\n", b1, b1) + // 统计字符串中 中文字符数量 + var count int + for _, char := range s2 { + if unicode.Is(unicode.Han, char) { + count++ + } + } + fmt.Printf("%s 汉字数量 => %d\n", s2, count) + fmt.Println("=========================================================") // 多行字符串