6

[Golang] Check If The Rune is Chinese Character

 2 years ago
source link: https://siongui.github.io/2018/10/11/go-check-if-rune-value-is-chinese-character/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Golang] Check If The Rune is Chinese Character

October 11, 2018

Given a rune value, check if the rune is a Chinese character.

Run Code on Go Playground

package main

import (
      "fmt"
      "unicode"
)

func main() {
      str := "hello, 世界"
      for _, runeValue := range str {
              if unicode.Is(unicode.Han, runeValue) {
                      fmt.Println(string(runeValue), "is a Chinese character")
              } else {
                      fmt.Println(string(runeValue), "is not a Chinese character")
              }
      }
}

Output

h is not a Chinese character
e is not a Chinese character
l is not a Chinese character
l is not a Chinese character
o is not a Chinese character
, is not a Chinese character
  is not a Chinese character
世 is a Chinese character
界 is a Chinese character

Tested on: Go Playground


References:

[1][Golang] Iterate Over UTF-8 Strings (non-ASCII strings)[2]unicode - The Go Programming Language[3][Golang] First Letter of Chinese Character Pinyin[4][Golang] Calculate String Length[5][Golang] Get UTF-8 String Width[6][Golang] Sort String by Character[7]I need help understanding runes : golang

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK