1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| package main
import ( "fmt" "strings" "strconv" )
func main() {
//判断字符串是否以 xx开头 var str string = "This is an example of a string" r := strings.HasPrefix(str,"Th") fmt.Println(r) r1 := strings.HasPrefix(str,"th") fmt.Println(r1)
//以什么结尾 r2 := strings.HasSuffix(str,"ing") fmt.Println(r2)
//是否包含字符串判断 rr1 := strings.Contains(str,"ex") fmt.Println(rr1)
//返回 i 在字符串中第一个字符的索引 -1 标示不在字符串中 rr2 := strings.Index(str,"g") fmt.Println(rr2)
//返回 i 在字符串中最后出现的位置 -1 标示不在字符串中 r3 := strings.LastIndex(str,"g") fmt.Print(r3)
r4 := strings.IndexRune(str,'i') fmt.Println(r4)
//字符串替换 strings.Replace(str, old, new, n) string n 设置替换个数 -1时替换全部 r5 := strings.Replace(str,"i"," ii",2) fmt.Println(r5)
//统计字符i在字符串中出现的次数 r6 := strings.Count(str,"i") fmt.Println(r6)
//重复字符串 r7 := strings.Repeat(str,5) fmt.Println(r7)
//小写转换 r8 := strings.ToLower(str) fmt.Println(r8)
//大写转换 r9 := strings.ToUpper(str) fmt.Println(r9)
//去除字符串开头或结尾 "g" s := strings.Trim(str,"g") fmt.Println(s)
r10 :=strings.Split("his",str) fmt.Print(r10,str)
r11 := strings.Fields(str) fmt.Println(r) for _,v := range r11 { fmt.Printf("%s \n",v) }
str = "GO1|The ABC of Go|25" r12 := strings.Split(str,"|") fmt.Println(r ) for _, val := range r12 { fmt.Printf("%s \n", val) } fmt.Println(strings.Join(r12 ,";"))
//数字转字符串 fmt.Println(strconv.Itoa(10)) //字符串转数字 fmt.Println(strconv.Atoi("10"))
}
|