具体使用,请看代码
package main
//需要导入字符串操作包strings
import (
"fmt"
"strings"
)
func main() {
s1 := "stevennamezhao"
//Contains的使用:判断是否含有字串,在就返回true
//Contains(s string ,str string)bool
fmt.Println(strings.Contains(s1, "name"))
//Join:把字符串通过sep(符号)连接在slice切片后面,返回一个string
//func Join(a []string, sep string) string
slice := []string{"qww", "steven", "zhao"}
s2 := strings.Join(slice, "@")
fmt.Println(s2) //qww@steven@zhao
//index:z在主串中查找字串的索引位置
//func Index(s, substr string) int
index := strings.Index(s1, "name")
fmt.Println(index) //6
//repeat:重复字符串count次,并返回字符串中
//func Repeat(s string, count int) string
s3 := "steven is a good person"
s3 = strings.Repeat(s3, 3)
fmt.Println(s3) //steven is a good personsteven is a good personsteven is a good person
//replace:替换字符串的特定字符,n,表示替换的个数,-1表示全部替换
//func Replace(s, old, new string, n int) string
s3 = strings.Replace(s3, "steven", "zhao", -1)
fmt.Println(s3) //zhao is a good personzhao is a good personzhao is a good person
//split:把字符串按照sep(规则)分割
//func Split(s, sep string) []string
s4 := "steven and zhao are good people"
s5 := strings.Split(s4, " ") //以空格分割,返回的是一个切片
fmt.Println(s5)
//trim:在字符串的头部和尾部,去除cutset指定字符串()去除两边含有连续的的cutset字符
//func Trim(s string, cutset string) string
s6 := strings.Trim(s4, "stel")
fmt.Println(s6) //ven and zhao are good peop
//fields;去除字符串中的空格,并按照空格分隔返回
//func Fields(s string) []string
s7 := strings.Fields(s4) //返回的是切片
fmt.Println(s7) //[steven and zhao are good people]
}