1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
// 返回字符串s包含字符串substr的个数
s := "hello word"
fmt.Printf("字符串:%s,o出现数量: %d\n",s,strings.Count(s,"o"))
// 判断字符串s是否包含substr字符串
fmt.Printf("字符串:%s 是否包含%s ? %t \n",s,"word",strings.Contains(s,"word"))
fmt.Printf("字符串:%s 是否包含%s ? %t \n",s,"go",strings.Contains(s,"go"))
// 判断字符串s是否包含chars字符串中的任意一个字符
fmt.Printf("字符串:%s 是否包含%s中的任意一个字符 ? %t \n",s,"go",strings.ContainsAny(s,"go"))
fmt.Printf("字符串:%s 是否包含%s中的任意一个字符 ? %t \n",s,"gg",strings.ContainsAny(s,"gg"))
// 判断字符串s是否包含unicode的码值r
r := 'w'
fmt.Printf("字符串:%s 是否包含unicode的码值%c? %t \n",s,r,strings.ContainsRune(s,r))
fmt.Printf("字符串:%s 是否包含unicode的码值%d? %t \n",s,119,strings.ContainsRune(s,119))
// 返回字符串s中字符串substr最后一次出现的位置
fmt.Printf("在字符串%s中,字符串%s最后一次出现的位置? %d \n",s,"o",strings.LastIndex(s,"o"))
// 返回字符串s中字符串substr首次出现的位置
fmt.Printf("在字符串%s中,字符串%s首次出现的位置? %d \n",s,"o",strings.Index(s,"o"))
// 返回字符串s中字符c首次出现的位置
var b byte = 'l'
fmt.Printf("在字符串%s中,字符%c首次出现的位置? %d \n",s,b,strings.IndexByte(s,b))
// 返回字符串s中字符c最后一次出现的位置
fmt.Printf("在字符串%s中,字符%c最后一次出现的位置? %d \n",s,b,strings.LastIndexByte(s,b))
// 返回unicode的码值r在字符串s中首次出现的位置
fmt.Printf("在字符串%s中,unicode的码值%d(%c)首次出现的位置? %d \n",s,104,104,strings.IndexRune(s,104))
// 返回字符串chars中的任意一个字符unicode码值,在s中首次出现的位置
s3 := "rd"
fmt.Printf("返回字符串%s中的任意一个字符unicode码值(%s)首次出现的位置? %d \n",s,s3,strings.LastIndexAny(s,s3))
// 判断字符串s是否有前缀prefix
a := "VIP001"
fmt.Printf("字符串:%s 是否有前缀%s ? %t \n",a,"vip",strings.HasPrefix(a,"vip"))
fmt.Printf("字符串:%s 是否有前缀%s ? %t \n",a,"VIP",strings.HasPrefix(a,"VIP"))
// 判断字符串s是否有后缀suffix
sn := "K011_Mn"
fmt.Printf("字符串:%s 是否有后缀%s ? %t \n",sn,"MN",strings.HasSuffix(sn,"MN"))
fmt.Printf("字符串:%s 是否有后缀%s ? %t \n",sn,"Mn",strings.HasSuffix(sn,"Mn"))
// 返回字符串s中满足函数f(r)==true,字符首次出现的位置 (判断第一个汉字的位置)
f := func(c rune) bool {
return unicode.Is(unicode.Han,c)
}
s4 := "go!中国人"
fmt.Printf("字符串:%s 首次出现汉字的位置%d \n",s4,strings.IndexFunc(s4,f))
fmt.Printf("字符串:%s 最后一次出现汉字的位置%d \n",s4,strings.LastIndexFunc(s4,f))
}
/** 输出
字符串:hello word,o出现数量: 2
字符串:hello word 是否包含word ? true
字符串:hello word 是否包含go ? false
字符串:hello word 是否包含go中的任意一个字符 ? true
字符串:hello word 是否包含gg中的任意一个字符 ? false
字符串:hello word 是否包含unicode的码值w? true
字符串:hello word 是否包含unicode的码值119? true
在字符串hello word中,字符串o最后一次出现的位置? 7
在字符串hello word中,字符串o首次出现的位置? 4
在字符串hello word中,字符l首次出现的位置? 2
在字符串hello word中,字符l最后一次出现的位置? 3
在字符串hello word中,unicode的码值104(h)首次出现的位置? 0
返回字符串hello word中的任意一个字符unicode码值(rd)首次出现的位置? 9
字符串:VIP001 是否有前缀vip ? false
字符串:VIP001 是否有前缀VIP ? true
字符串:K011_Mn 是否有后缀MN ? false
字符串:K011_Mn 是否有后缀Mn ? true
字符串:go!中国人 首次出现汉字的位置3
字符串:go!中国人 最后一次出现汉字的位置9
*/
|