diff --git a/examples/strings-runes/strings-runes.go b/examples/strings-runes/strings-runes.go new file mode 100644 index 0000000..cbac78e --- /dev/null +++ b/examples/strings-runes/strings-runes.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "unicode/utf8" +) + +// TODO: Thai hello, vowels + +func main() { + const hello = "สวัสดี" + foo(hello) +} + +func foo(s string) { + fmt.Println("Len:", len(s)) + + for i := 0; i < len(s); i++ { + fmt.Printf("%x ", s[i]) + } + fmt.Println() + + fmt.Println("Rune count:", utf8.RuneCountInString(s)) + + for idx, runeValue := range s { + fmt.Printf("%#U starts at %d\n", runeValue, idx) + } + + fmt.Println("\nUsing DecodeRuneInString") + for i, w := 0, 0; i < len(s); i += w { + runeValue, width := utf8.DecodeRuneInString(s[i:]) + fmt.Printf("%#U starts at %d\n", runeValue, i) + w = width + + examineRune(runeValue) + } +} + +func examineRune(r rune) { + if r == 't' { + fmt.Println("found tee") + } else if r == 'ส' { + fmt.Println("found so sua") + } +}