在线文档  >   Golang练习   >   字符串和rune类型

Go语言中的字符串是一个只读的byte切片。语言和标准库在字符串方面做了特殊处理,可以将其视为使用UTF-8编码的文本容器。
在其他语言中,字符串由“字符”组成。在Go中,字符的概念称为rune,它是表示Unicode编码的整数。
这篇Go博客文章是一个很好的入门教程。

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {

    // `s` 是一个 `string` 类型,赋予了泰语中单词“hello”的文本。
    // Go字符串文本使用UTF-8编码。
    const s = "สวัสดี"

    // 由于字符串等同于 `[]byte`,因此这将产生存储在其中的原始字节数的长度。
    fmt.Println("Len:", len(s))

    // 通过索引访问字符串,可以生成每个索引处的原始字节值。
    // 此循环生成构成 `s` 中utf-8编码的所有字节的十六进制值。
    for i := 0; i < len(s); i++ {
        fmt.Printf("%x ", s[i])
    }
    fmt.Println()

    // 要统计字符串中有多少个`rune`,我们可以使用 `utf8` 包。
    // 请注意,`RuneCountInString` 的运行时间取决于字符串的大小,因为它必须按顺序解码每个UTF-8 rune。
   // 一些泰语字符由多个UTF-8编码表示,因此这个计数的结果可能会令人惊讶。
    fmt.Println("Rune count:", utf8.RuneCountInString(s))

    // 使用 `range` 循环可以特殊处理字符串,并解码每个 `rune` 以及其在字符串中的偏移量。
    for idx, runeValue := range s {
        fmt.Printf("%#U starts at %d\n", runeValue, idx)
    }

    // 我们可以使用 `utf8.DecodeRuneInString` 函数显式地实现相同的遍历。
    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

        // 这演示了将 `rune` 值传递给函数的方法。
        examineRune(runeValue)
    }
}

func examineRune(r rune) {

    // 用单引号括起来的值是`rune字面量`。我们可以直接将 `rune` 值与rune字面量进行比较。
    if r == 't' {
        fmt.Println("found tee")
    } else if r == 'ส' {
        fmt.Println("found so sua")
    }
}

运行结果如下:

$ go run strings-and-runes.go
Len: 18
e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5 
Rune count: 6
U+0E2A 'ส' starts at 0
U+0E27 'ว' starts at 3
U+0E31 'ั' starts at 6
U+0E2A 'ส' starts at 9
U+0E14 'ด' starts at 12
U+0E35 'ี' starts at 15

Using DecodeRuneInString
U+0E2A 'ส' starts at 0
found so sua
U+0E27 'ว' starts at 3
U+0E31 'ั' starts at 6
U+0E2A 'ส' starts at 9
found so sua
U+0E14 'ด' starts at 12
U+0E35 'ี' starts at 15