package main
import (
"fmt"
"strings"
)
func main() {
str := " This is an example of a string "
prefix := "T"
if strings.HasPrefix(str, prefix) {
fmt.Printf("the string '%s' have prefix %s\n", str, prefix)
}
suffix := "g"
if strings.HasSuffix(str, suffix) {
fmt.Printf("the string '%s' have suffix %s\n", str, suffix)
}
substr := "example"
if strings.Contains(str, substr) {
fmt.Printf("the string '%s' contains '%s'\n", str, substr)
}
pos := strings.Index(str, substr)
fmt.Printf("字符串 '%s' 在字符串 '%s' 中的位置是%d\n", substr, str, pos)
lastPos := strings.LastIndex(str, "a")
fmt.Printf("字符串 'a' 在字符串 '%s' 中最后出现的位置是%d\n", str, lastPos)
runePos := strings.IndexRune(str, 'a')
fmt.Printf("字符'a' 在字符串 '%s' 中出现的位置是%d\n", str, runePos)
count := strings.Count(str, "a")
fmt.Printf("the string 'a' has appeared %d times in '%s'\n", count, str)
repeatStr := "repeat string!"
repeatStr = strings.Repeat(repeatStr, 2)
fmt.Printf("重复后的字符串为'%s'\n", repeatStr)
upperStr := strings.ToUpper(str)
fmt.Printf("字符串大写:'%s'\n", upperStr)
lowerStr := strings.ToLower(upperStr)
fmt.Printf("字符串小写:'%s'\n", lowerStr)
newStr := strings.TrimSpace(str)
fmt.Printf("原始字符串为:'%s'\n", str)
fmt.Printf("去掉两边的空格后字符串为:'%s'\n", newStr)
cutset1 := "Ths"
s1 := "ThisisGThiss"
newStr1 := strings.Trim(s1, cutset1)
fmt.Printf("'%s'去掉两边的'%s'后字符串为:'%s'\n", s1, cutset1, newStr1)
s2 := "ThisisGThiss"
cutset2 := "This"
newStr2 := strings.TrimLeft(s2, cutset2)
fmt.Printf("'%s'去掉左边的'%s'后字符串为:'%s'\n", s2, cutset2, newStr2)
s4 := "ThisisGisThiss"
cutset4 := "This"
newStr4 := strings.TrimPrefix(s4, cutset4)
fmt.Printf("去掉'%s'的前缀'%s'后的字符串为:'%s'\n", s4, cutset4, newStr4)
newStr3 := strings.TrimRight(s2, cutset2)
fmt.Printf("'%s'去掉右边的'%s'后字符串为:'%s'\n", s2, cutset2, newStr3)
s5 := "ThisisGisThis"
cutset5 := "This"
newStr5 := strings.TrimSuffix(s5, cutset5)
fmt.Printf("去掉'%s'的后缀'%s'后的字符串为:'%s'\n", s5, cutset5, newStr5)
s6 := "sss,s s"
fmt.Printf("Fields are: %q\n", strings.Fields(s6))
fmt.Printf("Fields are: %q\n", strings.Split(s6, ","))
slice1 := strings.Split(s6, ",")
fmt.Printf("The string is: %q\n", strings.Join(slice1, "#"))
}