package main import ( "fmt" "strings" ) func main() { //判断字符串1是否包含字符串2,如果包含则返回true , 不包含则返回false fmt.Println(strings.Contains("hello go", "hello")) fmt.Println(strings.Contains("hello go", "abc")) //joins的组合 s:=[]string{"abc", "hello", "mike", "go"} buf := strings.Join(s, "@") fmt.Println("buf = ", buf) //index, 查找子串的位置 fmt.Println(strings.Index("abcdhello", "hello")) fmt.Println(strings.Index("abcd", "e")) //如果不包含子串的话则返回-1 buf = strings.Repeat("go", 3) //重复三次打印 fmt.Println("buf = ", buf) buf = "hello@go@mike" s2 := strings.Split(buf, "@") //打印以"@"分隔的结果 fmt.Println("buf = ", s2) buf = strings.Trim(" are you ok ", " ") //去掉字符串两边的空格 fmt.Printf("buf = #%s# ", buf) buf1 := strings.Fields(" are you ok ") //去掉所有的空格后返回一个切片 for i, data := range buf1{ fmt.Println(i, ", ", data) } }
执行的结果为
false buf = abc@hello@mike@go 4 -1 buf = gogogo buf = [hello go mike] buf = #are you ok# 0 , are 1 , you 2 , ok