更多的还是去官方文档里去看:https://studygolang.com/pkgdoc
Contains:
判断字符串中是否包含指定字符串
演示:
func main() {str1 := "itzhuzhu"result := strings.Contains(str1, "zhu")fmt.Println(result)
}
join:
字符串连接
演示:
func main() {str2 := []string{"a", "b", "c", "d", "e", "f"}join := strings.Join(str2, "")fmt.Println("join:", join)
}
Index:
查找指定字符串所在的位置,返回索引,找不到返回-1
演示:
func main() {str3 := "itzhuzhu"result3 := strings.Index(str3, "zhu")fmt.Println("result:", result3)
}
Repeat:
重复字符串次数,返回重复的字符串
演示:
func main() {str4 := "itzhuzhu"result4 := strings.Repeat(str4, 3)fmt.Println("result:", result4)
}
Replace:
func Replace(s, old, new string, n int) string
在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
演示:
func main() {str5 := "itzhuzhu"strings.Replace(str5, "z", "Z", 1)// 替换第一个strings.Replace(str5, "z", "Z", -1)// 全部替换
}
Split:
字符串分割,返回切片
演示:
func main() {str6 := "itzhuzhu@163.com"split := strings.Split(str6, "@")fmt.Println("split:", split)
}
Trim:
func Trim(s string, cutset string) string
t在s字符串的头部和尾部去除cutset指定的字符串
演示:
func main() {str7 := " itzhuzhu@163.com "trim := strings.Trim(str7, " ") // 去掉两头空格fmt.Println("trim:", trim)
}
Fields:
去除字符串的空格符,并且按照空格分割返回slice
演示:
func main() {str8 := " itzhuzhu@163.com "fields := strings.Fields(str8)fmt.Println("fields:", fields)
}