[英]Store unicode characters in golang
I'm creating a data structure for storing single unicode characters which I can then compare. 我正在创建一个数据结构来存储单个unicode字符,然后可以对其进行比较。
Two Questions: 两个问题:
What data types do I use? 我使用什么数据类型?
type ds struct { char Char // What should Char be so that I can safely compare two ds? }
I need a way to compare the first character of any two unicode strings. 我需要一种比较任何两个unicode字符串的第一个字符的方法。 Is there a simple way to do that?
有没有简单的方法可以做到这一点? Basically, how do I retrieve the first unicode character of a string?
基本上,我如何检索字符串的第一个unicode字符?
Like this: type Char rune
. 像这样:
type Char rune
。
Pay attention to "compare", that is a complicated thing with Unicode. 注意“比较”,这对于Unicode是一件复杂的事情。 While code points (
rune
s) are easy to compare numerically (U+0020 == U+0020; U+1234 < U+2345) this might or might not be what you want given case, combining characters and what else Unicode offers. 虽然代码点(
rune
)很容易进行数字比较(U + 0020 == U + 0020; U + 1234 <U + 2345),但这可能不是您想要的情况,它结合了字符和Unicode提供的其他功能。
To compare utf8 strings, you need to check their runevalue. 要比较utf8字符串,您需要检查其符文值。 Runevalue is int32 value of utf8 character.
Runevalue是utf8字符的int32值。 Use standard package "unicode/utf8".
使用标准软件包“ unicode / utf8”。 Pass "string[0:]" to get the first character
传递“ string [0:]”以获取第一个字符
test := "春节" runeValue, width := utf8.DecodeRuneInString(test[0:]) fmt.Println(runeValue,width) fmt.Printf("%#U %d", runeValue, runeValue)
Now you can compare runeValue of two strings's first character using == operator 现在,您可以使用==运算符比较两个字符串的第一个字符的runeValue
Also you need to store string in string if you want to store whole character. 另外,如果要存储整个字符,则需要在字符串中存储字符串。
type ds struct { char string // What should Char be so that I can safely compare two ds? }
Complete code demonstrating this: 完整的代码演示了这一点:
package main
import (
"fmt"
"unicode/utf8"
)
type ds struct {
char string // What should Char be so that I can safely compare two ds?
}
func main() {
fmt.Println("Hello, playground")
ds1 := ds{"春节"}
ds2 := ds{"春节"}
runeValue1, _ := utf8.DecodeRuneInString(ds1.char[0:])
runeValue2, _ := utf8.DecodeRuneInString(ds2.char[0:])
fmt.Printf("%#U %#U", runeValue1, runeValue2)
if runeValue1 == runeValue2 {
fmt.Println("\nFirst Char Same")
} else {
fmt.Println("\nDifferent")
}
}
From Volkers, answer, we can just use rune to compare. 在Volkers中,我们可以使用符文进行比较。
type Char rune
[]rune(str)[0]
[]rune(str)[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.