go-by-example/strings-and-runes/stringsandrunes.go
2023-12-18 03:40:05 -05:00

55 lines
No EOL
1.8 KiB
Go

package main
import (
"fmt"
"unicode/utf8"
)
func main() {
// s is a string assigned a literal value representing the
// word "hello" in the Thai language.
// Go string literals are UTF-8 encoded text.
const s = "สวัสดี"
//Since strings are equivalent to []byte,
//this will produce the length of the raw bytes stored within
fmt.Println("len:", len(s))
// indexing into a string produces the raw byte values at each index.
// This loop generates the hex values of all the bytes
// that constitute the code points in s.
for i := 0; i < len(s); i++ {
fmt.Printf("%x ", s[i])
}
fmt.Println()
// To count how many runes are in a string,
// we ca can use the utf8 package. Note that
// the run-time of RuneCountInString depends on the size of the string
// due to having to decode each rune sequentially.
// Some Thai characters are represented by multiple UTF-8 code points,
// so the result of this count may be suprising.
fmt.Println("Rune count:",utf8.RuneCountInString(s))
// A range loop handles strings specially and decodes each rune
// along with it's offset in the string
for idx, runeValue := range s {
fmt.Printf("%#U Starts at %d\n", runeValue, idx)
}
//we can achieve the same iteration by using the utf8.DecodeRuneInString function explicitly
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
// This demonstrates passing a rune to a func
examineRune(runeValue)
}
}
// Values enclosed in single quotes are rune literals.
// we can compare a rune value to a rune literal directly.
func examineRune(r rune) {
if r == 't' {
fmt.Println("found tee")
} else if r == 'ส' {
fmt.Println("found so sua")
}
}