Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 459
Matt Avatar asked Aug 08 '13 16:08

Matt


People also ask

Can u iterate through a string?

You can traverse a string as a substring by using the Python slice operator ([]). It cuts off a substring from the original string and thus allows to iterate over it partially. To use this method, provide the starting and ending indices along with a step value and then traverse the string.


2 Answers

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 6 

For strings, the range does more work for you, breaking out individual Unicode code points by parsing the UTF-8.

like image 79
Denys Séguret Avatar answered Oct 14 '22 17:10

Denys Séguret


To mirror an example given at golang.org, Go allows you to easily convert a string to a slice of runes and then iterate over that, just like you wanted to originally:

runes := []rune("Hello, 世界") for i := 0; i < len(runes) ; i++ {     fmt.Printf("Rune %v is '%c'\n", i, runes[i]) } 

Of course, we could also use a range operator like in the other examples here, but this more closely follows your original syntax. In any case, this will output:

Rune 0 is 'H' Rune 1 is 'e' Rune 2 is 'l' Rune 3 is 'l' Rune 4 is 'o' Rune 5 is ',' Rune 6 is ' ' Rune 7 is '世' Rune 8 is '界' 

Note that since the rune type is an alias for int32, we must use %c instead of the usual %v in the Printf statement, or we will see the integer representation of the Unicode code point (see A Tour of Go).

like image 41
Justin Kulikauskas Avatar answered Oct 14 '22 16:10

Justin Kulikauskas