您好,登录后才能下订单哦!
Contains
func Contains(s, substr string) bool
功能:字符串s中是否包含substr,返回bool值
示例代码:
fmt.Println(strings.Contains("seafood", "foo"))
fmt.Println(strings.Contains("seafood", "bar"))
fmt.Println(strings.Contains("seafood", ""))
fmt.Println(strings.Contains("", ""))
//运行结果:
//true
//false
//true
//true
Join
func Join(a []string, sep string) string
功能:字符串链接,把slice a通过sep链接起来
示例代码:
s := []string{"foo", "bar", "baz"}
fmt.Println(strings.Join(s, ", "))
//运行结果:foo, bar, baz
Index
func Index(s, sep string) int
功能:在字符串s中查找sep所在的位置,返回位置值,找不到返回-1
示例代码:
fmt.Println(strings.Index("chicken", "ken"))
fmt.Println(strings.Index("chicken", "dmr"))
//运行结果:
// 4
// -1
Repeat
func Repeat(s string, count int) string
功能:重复s字符串count次,最后返回重复的字符串
示例代码:
fmt.Println("ba" + strings.Repeat("na", 2))
//运行结果:banana
Replace
func Replace(s, old, new string, n int) string
功能:在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
示例代码:
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
//运行结果:
//oinky oinky oink
//moo moo moo
Split
func Split(s, sep string) []string
功能:把s字符串按照sep分割,返回slice
示例代码:
fmt.Printf("%q\n", strings.Split("a,b,c", ","))
fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
fmt.Printf("%q\n", strings.Split(" xyz ", ""))
fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
//运行结果:
//["a" "b" "c"]
//["" "man " "plan " "canal panama"]
//[" " "x" "y" "z" " "]
//[""]
Trim
func Trim(s string, cutset string) string
功能:在s字符串的头部和尾部去除cutset指定的字符串
示例代码:
fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
//运行结果:["Achtung"]
Fields
func Fields(s string) []string
功能:去除s字符串的空格符,并且按照空格分割返回slice
示例代码:
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
//运行结果:Fields are: ["foo" "bar" "baz"]
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。