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
| package main
import ( "fmt" "strings" "unicode" )
func main() { s := "hello word" fmt.Printf("字符串:%s,o出现数量: %d\n",s,strings.Count(s,"o")) fmt.Printf("字符串:%s 是否包含%s ? %t \n",s,"word",strings.Contains(s,"word")) fmt.Printf("字符串:%s 是否包含%s ? %t \n",s,"go",strings.Contains(s,"go")) fmt.Printf("字符串:%s 是否包含%s中的任意一个字符 ? %t \n",s,"go",strings.ContainsAny(s,"go")) fmt.Printf("字符串:%s 是否包含%s中的任意一个字符 ? %t \n",s,"gg",strings.ContainsAny(s,"gg")) r := 'w' fmt.Printf("字符串:%s 是否包含unicode的码值%c? %t \n",s,r,strings.ContainsRune(s,r)) fmt.Printf("字符串:%s 是否包含unicode的码值%d? %t \n",s,119,strings.ContainsRune(s,119)) fmt.Printf("在字符串%s中,字符串%s最后一次出现的位置? %d \n",s,"o",strings.LastIndex(s,"o")) fmt.Printf("在字符串%s中,字符串%s首次出现的位置? %d \n",s,"o",strings.Index(s,"o")) var b byte = 'l' fmt.Printf("在字符串%s中,字符%c首次出现的位置? %d \n",s,b,strings.IndexByte(s,b)) fmt.Printf("在字符串%s中,字符%c最后一次出现的位置? %d \n",s,b,strings.LastIndexByte(s,b))
fmt.Printf("在字符串%s中,unicode的码值%d(%c)首次出现的位置? %d \n",s,104,104,strings.IndexRune(s,104)) s3 := "rd" fmt.Printf("返回字符串%s中的任意一个字符unicode码值(%s)首次出现的位置? %d \n",s,s3,strings.LastIndexAny(s,s3))
a := "VIP001" fmt.Printf("字符串:%s 是否有前缀%s ? %t \n",a,"vip",strings.HasPrefix(a,"vip")) fmt.Printf("字符串:%s 是否有前缀%s ? %t \n",a,"VIP",strings.HasPrefix(a,"VIP"))
sn := "K011_Mn" fmt.Printf("字符串:%s 是否有后缀%s ? %t \n",sn,"MN",strings.HasSuffix(sn,"MN")) fmt.Printf("字符串:%s 是否有后缀%s ? %t \n",sn,"Mn",strings.HasSuffix(sn,"Mn")) f := func(c rune) bool { return unicode.Is(unicode.Han,c) } s4 := "go!中国人" fmt.Printf("字符串:%s 首次出现汉字的位置%d \n",s4,strings.IndexFunc(s4,f)) fmt.Printf("字符串:%s 最后一次出现汉字的位置%d \n",s4,strings.LastIndexFunc(s4,f)) }
|