gobyexample/runes/runes.go

27 lines
553 B
Go
Raw Permalink Normal View History

2024-04-27 05:45:44 +00:00
package runes
import (
"fmt"
"unicode/utf8"
)
func Runes() {
s := "Hello, world!"
fmt.Printf("%s has %d runes\n", s, utf8.RuneCountInString(s))
s = "നമസ്കാരം"
fmt.Printf("%s has %d runes\n", s, utf8.RuneCountInString(s))
fmt.Printf("Iterating through %s:\n", s)
for i, val := range s {
fmt.Printf("%2d = %#U\n", i, val)
}
fmt.Printf("Decoding %s:\n", s)
for i, w := 0, 0; i < len(s); i += w {
value, width := utf8.DecodeRuneInString(s[i:])
fmt.Printf("Decoded rune %#U of size %d\n", value, width)
w = width
}
}