rune

How can I iterate over a string by runes in Go?

▼魔方 西西 提交于 2021-02-17 11:12:28
问题 I wanted to this: for i := 0; i < len(str); i++ { dosomethingwithrune(str[i]) // takes a rune } But it turns out that str[i] has type byte ( uint8 ) rather than rune . How can I iterate over the string by runes rather than bytes? 回答1: See this example from Effective Go : for pos, char := range "日本語" { fmt.Printf("character %c starts at byte position %d\n", char, pos) } This prints : character 日 starts at byte position 0 character 本 starts at byte position 3 character 語 starts at byte position

How can I iterate over a string by runes in Go?

笑着哭i 提交于 2021-02-17 11:11:01
问题 I wanted to this: for i := 0; i < len(str); i++ { dosomethingwithrune(str[i]) // takes a rune } But it turns out that str[i] has type byte ( uint8 ) rather than rune . How can I iterate over the string by runes rather than bytes? 回答1: See this example from Effective Go : for pos, char := range "日本語" { fmt.Printf("character %c starts at byte position %d\n", char, pos) } This prints : character 日 starts at byte position 0 character 本 starts at byte position 3 character 語 starts at byte position

What are Go's rules for comparing bytes with runes?

落花浮王杯 提交于 2020-12-25 03:59:42
问题 I've discovered the following peculiarity: b := "a"[0] r := 'a' fmt.Println(b == r) // Does not compile, cannot compare byte and rune fmt.Println("a"[0] == 'a') // Compiles and prints "true" How does this work? 回答1: This is an example of untyped constants. From the docs: Untyped boolean, numeric, and string constants may be used as operands wherever it is legal to use an operand of boolean, numeric, or string type, respectively. Except for shift operations, if the operands of a binary

What are Go's rules for comparing bytes with runes?

梦想与她 提交于 2020-12-25 03:58:33
问题 I've discovered the following peculiarity: b := "a"[0] r := 'a' fmt.Println(b == r) // Does not compile, cannot compare byte and rune fmt.Println("a"[0] == 'a') // Compiles and prints "true" How does this work? 回答1: This is an example of untyped constants. From the docs: Untyped boolean, numeric, and string constants may be used as operands wherever it is legal to use an operand of boolean, numeric, or string type, respectively. Except for shift operations, if the operands of a binary

What are Go's rules for comparing bytes with runes?

痞子三分冷 提交于 2020-12-25 03:58:26
问题 I've discovered the following peculiarity: b := "a"[0] r := 'a' fmt.Println(b == r) // Does not compile, cannot compare byte and rune fmt.Println("a"[0] == 'a') // Compiles and prints "true" How does this work? 回答1: This is an example of untyped constants. From the docs: Untyped boolean, numeric, and string constants may be used as operands wherever it is legal to use an operand of boolean, numeric, or string type, respectively. Except for shift operations, if the operands of a binary

What are Go's rules for comparing bytes with runes?

∥☆過路亽.° 提交于 2020-12-25 03:58:23
问题 I've discovered the following peculiarity: b := "a"[0] r := 'a' fmt.Println(b == r) // Does not compile, cannot compare byte and rune fmt.Println("a"[0] == 'a') // Compiles and prints "true" How does this work? 回答1: This is an example of untyped constants. From the docs: Untyped boolean, numeric, and string constants may be used as operands wherever it is legal to use an operand of boolean, numeric, or string type, respectively. Except for shift operations, if the operands of a binary

Golang converting from rune to string

半城伤御伤魂 提交于 2020-07-31 17:20:09
问题 I have the following code, it is supposed to cast a rune into a string and print it. However, I am getting undefined characters when it is printed. I am unable to figure out where the bug is: package main import ( "fmt" "strconv" "strings" "text/scanner" ) func main() { var b scanner.Scanner const a = `a` b.Init(strings.NewReader(a)) c := b.Scan() fmt.Println(strconv.QuoteRune(c)) } 回答1: That's because you used Scanner.Scan() to read a rune but it does something else. Scanner.Scan() can be

Golang converting from rune to string

谁说胖子不能爱 提交于 2020-07-31 17:09:32
问题 I have the following code, it is supposed to cast a rune into a string and print it. However, I am getting undefined characters when it is printed. I am unable to figure out where the bug is: package main import ( "fmt" "strconv" "strings" "text/scanner" ) func main() { var b scanner.Scanner const a = `a` b.Init(strings.NewReader(a)) c := b.Scan() fmt.Println(strconv.QuoteRune(c)) } 回答1: That's because you used Scanner.Scan() to read a rune but it does something else. Scanner.Scan() can be

What is a rune?

家住魔仙堡 提交于 2019-12-18 09:55:40
问题 What is a rune in Go? I've been googling but Golang only says in one line: rune is an alias for int32 . But how come integers are used all around like swapping cases? The following is a function swapcase. What is all the <= and - ? And why doesn't switch have any arguments? && should mean and but what is r <= 'z' ? func SwapRune(r rune) rune { switch { case 'a' <= r && r <= 'z': return r - 'a' + 'A' case 'A' <= r && r <= 'Z': return r - 'A' + 'a' default: return r } } Most of them are from

How to decode a string containing backslash-encoded Unicode characters?

不羁的心 提交于 2019-12-10 15:59:30
问题 I have a string stored as a : a := `M\u00fcnchen` fmt.Println(a) // prints "M\u00fcnchen" b := "M\u00fcnchen" fmt.Println(b) // prints "München" Is there a way I can convert a into b ? 回答1: You can use strconv.Unquote for this: u := `M\u00fcnchen` s, err := strconv.Unquote(`"` + u + `"`) if err != nil { // .. } fmt.Printf("%v\n", s) Outputs: München 来源: https://stackoverflow.com/questions/35519106/how-to-decode-a-string-containing-backslash-encoded-unicode-characters