go example 18: Strings and Runes

golang的字符串是只读的字节数组切片。go语言和标准库把字符串当成按照UTF8文本编码的容器。其他语言字符串理解成字符“characters”。在go中字符的概念是“rune ”- 它是一个表示 Unicode 码的整数。

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	// s是字符串,被赋值了泰语的hello。
	const s = "สวัสดี"

	// 因为字符串是字节数组[]byte,因此有长度字段
	fmt.Println("Len:", len(s))
	// print
	// Len: 18

	// 遍历字符数组中的每个字符,生成十六进制。
	for i := 0; i < len(s); i++ {
		fmt.Printf("%x ", s[i])
	}
	fmt.Println()
	// print
	// e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5

	// 计算有多少字符“runes”。RuneCountInString会按照顺序解码每个UTF8的字符rune。
	// 一些泰语字符需要用多个UTF8码表示,所以得到长度结果很奇怪。
	fmt.Println("Rune count:", utf8.RuneCountInString(s))
	// print
	// Rune count: 6

	// range处理字符串,解码每个rune,可以自动处理好每个偏移量。%#U会输出Unicode码以及字面量
	for idx, runeValue := range s {
		fmt.Printf("%#U starts at %d\n", runeValue, idx)
	}
	// print
	// 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

	// 可以用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)
		// print
		// U+0E2A 'ส' starts at 05
		// 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
	}

}

// 用单引号包住的值是rune字面值,可以用rune 值和字面值进行比较。
func examineRune(r rune) {
	if r == 't' {
		fmt.Println("found tee")
	} else if r == 'ส' {
		fmt.Println("found so sua")
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值