1:Contains(s,substr string) bool:字符串s是否包含substr,包含返回true,不包含返回false
flag := strings.Contains("hello world", "wor")
if flag {
fmt.Println("str is contain substr")
} else {
fmt.Println("str is not contain substr")
}
2:ContainAny(s ,chars string) bool:字符串s是否包含Unicode chars中任意字符,包含返回true,不包含返回false
flag = strings.ContainsAny("hello world", "h&w&o")
if flag {
fmt.Println("str is contain any chars")
} else {
fmt.Println("str is not contain chars")
}
3:ContainsRune(s string,r rune)bool :判断字符串s是否包含ASCII字符r,包含返回true,不包含返回false
flag = strings.ContainsRune("hello world", 'd')
if flag {
fmt.Println("containsRune exist")
} else {
fmt.Println("containrune not exist")
}
4:LastIndex(s,substr string) int:返回子串substr在父串s最后一次出现的位置,存在返回下标,不存在返回-1
index := strings.LastIndex("hello world", "o")
if index != -1 {
fmt.Println("LastIndex exist", index)
} else {
fmt.Println("LastIndex not exist")
}
5:IndexRune(s string,r sune)int:返回ASCII字符r,在父串中第一次出现的位置,成功返回索引,失败返回-1
index = strings.IndexRune("hello world", 'o')
if index != -1 {
fmt.Println("IndexRune exist", index)
} else {
fmt.Println("IndexRune not exist")
}
6:IndexAny(s,chars string)int:返回chars中任意字符出现在父串s的第一次出现的位置,成功返回索引,失败,返回-1
index = strings.IndexAny("hello world", "h&w&o")
if index != -1 {
fmt.Println("IndexAny exist :", index)
} else {
fmt.Println("Index not exist")
}
7:LastIndexAny(s ,chars string) int:返回chars中任意字符出现在父串s的最后一次出现的位置,成功返回索引,失败,返回-1
index = strings.LastIndexAny("hello world", "h&w&o")
if index != -1 {
fmt.Println("LastIndexAny exist :", index)
} else {
fmt.Println("LastIndexAny not exist")
}
8:LastIndexByte(s string,c byte) int:返回字节c最后一次在父串str中出现的索引,成功,返回索引,失败,返回-1
var c byte = 'o'
index = strings.LastIndexByte("hello world", c)
if index != -1 {
fmt.Println("LastIndexByte exist :", index)
} else {
fmt.Println("LastIndexByte not exist")
}
9:SplitN(s,sep string,n int)[]string:将字符串s按照sep子串划分成子串(子串不包含sep串)
(n >0 ):一直划分,直到划分到n个串
(n == 0):nil
(n < 0):划分到结束,返回所有子串
str = "helloworld hellogolang helloprograming hellotest
hellostring hello"
sep := "hello"
substring1 := strings.SplitN(str, sep, 0)
fmt.Println("SplitN n == 0:", substring1)
substring2 := strings.SplitN(str, sep, 4)
fmt.Println("SplitN n > 0:", substring2)
substring3 := strings.SplitN(str, sep, -1)
fmt.Println("SplitN n < 0:", substring3)
10:SplitAfterN(s,sep string,n int)[]stsring:将字符串s按照sep之后划分成子串(子串包含sep串)
(n >0 ):一直划分,直到划分到n个串
(n == 0):nil
(n < 0):划分到结束,返回所有子串
str = "helloworld hellogolang helloprograming hellotest
hellostring hello"
sep := "hello"
substring4 := strings.SplitAfterN(str, sep, 0)
fmt.Println("SplitAfterN n == 0:", substring4)
substring5 := strings.SplitAfterN(str, sep, 4)
fmt.Println("SplitAfterN n > 0:", substring5)
substring6 := strings.SplitAfterN(str, sep, -1)
fmt.Println("SplitAfterN n < 0 :", substring6)
11:Split(s,sep string)[]string:将字符串s按照sep划分成子串,直到划分结束,等同于SplitN(s,sep string,-1)
str = "helloworld hellogolang helloprograming hellotest
hellostring hello"
sep := "hello"
substring7 := strings.Split(str, sep)
fmt.Println("Split :", substring7)
12:SplitAfter(s,sep string)[]string:将字符串按照sep之后划分成子串,直到划分结束返回所有子串等同于SplitAfterN(s,sep,-1)
str = "helloworld hellogolang helloprograming hellotest
hellostring hello"
sep := "hello"
substring8 := strings.SplitAfter(str, sep)
fmt.Println("SplitAfter :", substring8)