Strings and Runes

本文详细介绍了Go语言中字符串的各种操作,包括检查子字符串、计数、判断前缀和后缀、查找位置、拼接、重复、替换、分割、大小写转换以及格式化输出。还探讨了字符串长度的正确计算方法,以及如何遍历和比较字符串中的字符。通过实例展示了这些方法的用法,对于理解和操作Go中的字符串非常有帮助。
摘要由CSDN通过智能技术生成

字符串方法

  • 字符串是否包含子字符串
fmt.Println("Contains:", strings.Contains("test", "es"))

Contains: true

  • 字符串包含子字符串数量
fmt.Println("Count:", strings.Count("test", "t"))

Count: 2

  • 字符串是否包含前缀
fmt.Println("HasPrefix:", strings.HasPrefix("test", "te"))

HasPrefix: true

  • 字符串是否包含后缀
fmt.Println("HasSuffix:", strings.HasSuffix("test", "st"))

HasSuffix: true

  • 字符串中子字符串的位置
fmt.Println("Index:", strings.Index("test", "e"))

Index: 1

  • 字符串拼接
fmt.Println("Join:", strings.Join([]string{"a", "b"}, "-"))

Join: a-b

  • 字符串重复
fmt.Println("Repeat:", strings.Repeat("a", 5))

Repeat: aaaaa

  • 字符串替换子字符串
fmt.Println("Replace:", strings.Replace("foo", "o", "0", -1))
fmt.Println("Replace:", strings.Replace("foo", "o", "0", 1))

Replace: f00
Replace: f0o

第4个参数表示替换的数量。参数小于0,即没有数量限制,否则,仅替换指定数量的子字符串。

  • 字符串分割
fmt.Println("Split:", strings.Split("a-b-c-d-e", "-"))

Split: [a b c d e]

  • 字符串大小写转换
fmt.Println("ToLower:", strings.ToLower("TEST"))
fmt.Println("ToUpper:", strings.ToUpper("test"))

ToLower: test
ToUpper: TEST

字符串格式化

  • 格式化结构体
p := point{1, 2}
fmt.Printf("struct1: %v\n", p)
fmt.Printf("struct2: %+v\n", p)
fmt.Printf("struct3: %#v\n", p)

struct1: {1 2}
struct2: {x:1 y:2}
struct3: main.point{x:1, y:2}

  • 格式化数据类型
fmt.Printf("type: %T\n", "string")
fmt.Printf("type: %T\n", rune(10))
fmt.Printf("type: %T\n", byte(10))
fmt.Printf("type: %T\n", int64(10))
fmt.Printf("type: %T\n", float64(10))
fmt.Printf("type: %T\n", point{1, 2})

type: string
type: int32
type: uint8
type: int64
type: float64
type: main.point

  • 格式化基本类型数据
fmt.Printf("bool: %t\n", true)
fmt.Printf("int: %d\n", 123)
fmt.Printf("bin: %b\n", 14)
fmt.Printf("char: %c\n", 33)
fmt.Printf("hex: %x\n", 456)
fmt.Printf("float1: %f\n", 78.9)
fmt.Printf("float2: %e\n", 123400000.0)
fmt.Printf("float3: %E\n", 123400000.0)

bool: true
int: 123
bin: 1110
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08

  • 格式化字符串类型数据
fmt.Printf("str1: %s\n", "\"string\"")
fmt.Printf("str2: %q\n", "\"string\"")
fmt.Printf("str3: %v\n", "\"string\"")
fmt.Printf("str4: %x\n", "hex this")

str1: “string”
str2: ““string””
str3: “string”
str4: 6865782074686973

  • 格式化指针
p := point{1, 2}
fmt.Printf("pointer: %p\n", &p)

pointer: 0xc0000b2010

  • 格式化对齐
fmt.Printf("width1: |%6d|%6d|\n", 12, 345)
fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)
fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
fmt.Printf("width4: |%6s|%6s|\n", "foo", "b")
fmt.Printf("width5: |%-6s|%-6s|\n", "foo", "b")

width1: | 12| 345|
width2: | 1.20| 3.45|
width3: |1.20 |3.45 |
width4: | foo| b|
width5: |foo |b |

  • 格式化字符串获取
s := fmt.Sprintf("sprintf: a %s", "string")
fmt.Println(s)

sprintf: a string

  • 格式化字符串并写入命令行
fmt.Fprintf(os.Stderr, "io: an %s\n", "error")

io: an error

字符串长度

s := "学而不思则罔,思而不学则殆。"
fmt.Println(len(s))

42

fmt.Println(utf8.RuneCountInString(s))

14

获取字符串长度应使用utf8.RuneCountInString

字符串遍历

  1. 直接遍历
for i := 0; i < len(s); i++ {
	fmt.Printf("%x ", s[i])
}

e5 ad a6 e8 80 8c e4 b8 8d e6 80 9d e5 88 99 e7 bd 94 ef bc 8c e6 80 9d e8 80 8c e4 b8 8d e5 ad a6 e5 88 99 e6 ae 86 e3 80 82

  1. range迭代
for i, v := range s {
	fmt.Printf("%d -> %#U\n", i, v)
}

0 -> U+5B66 ‘学’
3 -> U+800C ‘而’
6 -> U+4E0D ‘不’
9 -> U+601D ‘思’
12 -> U+5219 ‘则’
15 -> U+7F54 ‘罔’
18 -> U+FF0C ‘,’
21 -> U+601D ‘思’
24 -> U+800C ‘而’
27 -> U+4E0D ‘不’
30 -> U+5B66 ‘学’
33 -> U+5219 ‘则’
36 -> U+6B86 ‘殆’
39 -> U+3002 ‘。’

  1. utf8.DecodeRuneInString获取字符宽度
for i := 0; i < len(s); {
	r, size := utf8.DecodeRuneInString(s[i:])
	fmt.Printf("%d -> %#U\n", i, r)
	i += size
}

0 -> U+5B66 ‘学’
3 -> U+800C ‘而’
6 -> U+4E0D ‘不’
9 -> U+601D ‘思’
12 -> U+5219 ‘则’
15 -> U+7F54 ‘罔’
18 -> U+FF0C ‘,’
21 -> U+601D ‘思’
24 -> U+800C ‘而’
27 -> U+4E0D ‘不’
30 -> U+5B66 ‘学’
33 -> U+5219 ‘则’
36 -> U+6B86 ‘殆’
39 -> U+3002 ‘。’

从以上运行结果得出结论,建议使用rangeutf8.DecodeRuneInString

字符比较

func runeEqual(r rune) {
	if r == '学' {
		fmt.Println("it is 学")
	} else if r == '思' {
		fmt.Println("it is 思")
	} else {
		fmt.Printf("it is not 学 or 思, it is %#U\n", r)
	}
}
for _, v := range s {
	runeEqual(v)
}

it is 学
it is not 学 or 思, it is U+800C ‘而’
it is not 学 or 思, it is U+4E0D ‘不’
it is 思
it is not 学 or 思, it is U+5219 ‘则’
it is not 学 or 思, it is U+7F54 ‘罔’
it is not 学 or 思, it is U+FF0C ‘,’
it is 思
it is not 学 or 思, it is U+800C ‘而’
it is not 学 or 思, it is U+4E0D ‘不’
it is 学
it is not 学 or 思, it is U+5219 ‘则’
it is not 学 or 思, it is U+6B86 ‘殆’
it is not 学 or 思, it is U+3002 ‘。’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值