Starting a strings-runes example
For now just the code, no output, no comments For #411
This commit is contained in:
parent
2885fc2298
commit
ff399c7001
45
examples/strings-runes/strings-runes.go
Normal file
45
examples/strings-runes/strings-runes.go
Normal file
@ -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")
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user