温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

golang语言渐入佳境[22]-string检索类函数

发布时间:2020-07-02 18:31:08 来源:网络 阅读:202 作者:jonson_jackson 栏目:开发技术

string检索类函数

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main

import (
"fmt"
"strings"
"unicode"
)

/*
1、func Contains(s, substr string) bool
判断字符串s是否包含substr字符串

2、func ContainsAny(s, chars string) bool
判断字符串s是否包含chars字符串中的任一字符

3、func ContainsRune(s string, r rune) bool
判断字符串s是否包含unicode码值r

4、func Count(s, sep string) int
返回字符串s包含字符串sep的个数

5、func HasPrefix(s, prefix string) bool
判断字符串s是否有前缀字符串prefix

6、func HasSuffix(s, suffix string) bool
判断字符串s是否有后缀字符串suffix

7、func Index(s, sep string) int
返回字符串s中字符串sep首次出现的位置

8、func IndexAny(s, chars string) int
返回字符串chars中的任一unicode码值r在s中首次出现的位置

9、func IndexByte(s string, c byte) int
返回字符串s中字符c首次出现位置

10、func IndexFunc(s string, f func(rune) bool) int
返回字符串s中满足函数f(r)==true字符首次出现的位置

11、func IndexRune(s string, r rune) int
返回unicode码值r在字符串中首次出现的位置

12、func LastIndex(s, sep string) int
返回字符串s中字符串sep最后一次出现的位置

13、func LastIndexAny(s, chars string) int
返回字符串s中任意一个unicode码值r最后一次出现的位置

14、func LastIndexByte(s string, c byte) int
返回字符串s中字符c最后一次出现的位置

15、func LastIndexFunc(s string, f func(rune) bool) int
返回字符串s中满足函数f(r)==true字符最后一次出现的位置
*/
func main() {
TestLastIndexFunc()
}

func TestContains() {
fmt.Println(strings.Contains("seafood", "foo"))//true
fmt.Println(strings.Contains("seafood", "bar"))//false
fmt.Println(strings.Contains("seafood", ""))//true
fmt.Println(strings.Contains("", ""))//true
fmt.Println(strings.Contains("jonson郑2008", "郑"))//true
}

func TestContainsAny() {
fmt.Println(strings.ContainsAny("team", "i"))//false
fmt.Println(strings.ContainsAny("failure", "u & i"))//true
fmt.Println(strings.ContainsAny("foo", ""))//false
fmt.Println(strings.ContainsAny("", ""))//false
}

func TestContainsRune() {
fmt.Println(strings.ContainsRune("一丁丂", '丁'))//true
fmt.Println(strings.ContainsRune("一丁丂", 19969))//true
}

func TestCount() {
fmt.Println(strings.Count("cheese", "e"))//3
fmt.Println(strings.Count("one", ""))//4
}

func TestHasPrefix() {
fmt.Println(strings.HasPrefix("1000phone news", "1000"))//true
fmt.Println(strings.HasPrefix("1000phone news", "1000a"))//false
}

func TestHasSuffix() {
fmt.Println(strings.HasSuffix("1000phone news", "news"))//true
fmt.Println(strings.HasSuffix("1000phone news", "new"))//false
}

func TestIndex() {
fmt.Println(strings.Index("chicken", "ken"))//4
fmt.Println(strings.Index("chicken", "dmr"))//-1
}

func TestIndexAny() {
fmt.Println(strings.IndexAny("abcABC120", "教育基地A"))//3
}

func TestIndexByte() {
fmt.Println(strings.IndexByte("123abc", 'a'))//3
}

func TestIndexRune() {
fmt.Println(strings.IndexRune("abcABC120", 'C'))//5
fmt.Println(strings.IndexRune("It培训教育", '教'))//8
}

func TestIndexFunc() {
f := func(c rune) bool {
return unicode.Is(unicode.Han , c)
}
fmt.Println(strings.IndexFunc("Hello123,中国" , f))//9
}

func TestLastIndex() {
fmt.Println(strings.LastIndex("jonson learn english", "e"))//13
fmt.Println(strings.Index("go gopher", "go"))//0
fmt.Println(strings.LastIndex("go gopher", "go"))//3
fmt.Println(strings.LastIndex("go gopher", "rodent"))//-1
}

func TestLastIndexAny() {
fmt.Println(strings.LastIndexAny("chicken", "aeiouy"))//5
fmt.Println(strings.LastIndexAny("crwth", "aeiouy"))//-1
}

func TestLastIndexByte() {
fmt.Println(strings.LastIndexByte("abcABCA123", 'A'))//6
}


func TestLastIndexFunc() {
f := func(c rune) bool {
return unicode.Is(unicode.Han, c)
}
fmt.Println(strings.LastIndexFunc("Hello,世界", f))//9
fmt.Println(strings.LastIndexFunc("Hello,world中国人", f))//17
}
  • 本文链接: https://dreamerjonson.com/2018/11/30/golang-22-string-package/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

golang语言渐入佳境[22]-string检索类函数

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI