[go basics] Find the number of Chinese characters in the string of go basics

Foreword: I
recently found a job, and I am going to golang. The span is relatively large. I am learning the basics of go recently. The language is only a foundation. The principle is more important. Whatever the language is, the important thing is thinking. Come on! ! ! ! !

Title description

Use golang language to achieve. Find the number of Chinese characters in a string.

Problem-solving ideas

There is a unicode package in golang, and there is a unicode.Is method under this package. This method can be used to judge Chinese characters, so we can use range for loop to record the number of Chinese characters.

Code sample

package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main()  {
	s3 := "hello孙松"
	count := 0
	for _, c1:=range s3{
		if unicode.Is(unicode.Han,c1) {
			count++
		}
	}
	fmt.Printf("%d",count)
}

Published 197 original articles · praised 73 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_39397165/article/details/104997711