I am a beginer of golang.I wonder that how I can I get a unicode character from a string.
Like ,the string is "你好",how can I get the first character "你"?
From other place I get one way:
var str="你好"
runes_array := []rune(str)
fmt.Println(string(runes_array[0]))
It does work.
But I still have some questions:
1) Is there another way to make it?
2) Why in golang cannot use str[0] to get unicode character from string,
but get byte data?
First, you may want to read https://blog.golang.org/strings It will answer part of your questions.
A string in Go can contains arbitrary bytes. When you write str[i], the result is a byte, and the index is always a number of bytes.
Most of the time, strings are encoded in UTF-8 though. You have multiple ways to deal with UTF-8 encoding in a string.
For instance, you can use the for...range statement to iterate on a string rune by rune.
var first rune
for _,c := range str {
first = c
break
}
// first now contains the first rune of the string
You can also leverage the unicode/utf8 package. For instance:
r, size := utf8.DecodeRuneInString(str)
// r contains the first rune of the string
// size is the size of the rune in bytes
If the string is encoded in UTF-8, there is no direct way to access the nth rune of the string, because the size of the runes (in bytes) is not constant. If you need this feature, you can easily write your own helper function to do it (with for...range, or with the unicode/utf8 package).
这篇关于如何从golang中的字符串获取单个Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!