ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## strings ### 字符串的比较 > func Compare(a, b string) int Compare 函数,用于比较两个字符串的大小,如果两个字符串相等,返回为 0。如果 a 小于 b ,返回 -1 ,反之返回 1 。不推荐使用这个函数,直接使用 == != > < >= <= 等一系列运算符更加直观。 ``` s1 := "phper" s2 := "goper" b1 := strings.Compare(s1, s2) fmt.Println(b1)//1 b2 := strings.Compare(s2, s1) fmt.Println(b2)//-1 b3 := strings.Compare(s1, s1) fmt.Println(b3)//0 只有为0时是相等的 ``` >func EqualFold(s, t string) bool EqualFold 函数,计算 s 与 t 忽略字母大小写后是否相等。 ``` s3 := "go" s4 := "Go" b4 := strings.EqualFold(s3,s4) fmt.Println(b4)//true s5 := "go" s6 := "Go2" b5 := strings.EqualFold(s5,s6) fmt.Println(b5)//false ``` ### 是否存在某个字符或子串 >func Contains(s, substr string) bool 子串 substr 在 s 中,返回 true ``` s := "go,haha" fmt.Println(strings.Contains(s , "go")) //true ``` >func ContainsAny(s, chars string) bool chars 中任何一个 Unicode 代码点在 s 中,返回 true ``` s := "helloworld" fmt.Println(strings.ContainsAny(s, "h"))//true fmt.Println(strings.ContainsAny(s, "h & i"))//true fmt.Println(strings.ContainsAny(s, "e g"))//true fmt.Println(strings.ContainsAny(s, ""))//false fmt.Println(strings.ContainsAny("", ""))//false 即任意一个字符在字符串中存在就返回true ``` >func ContainsRune(s string, r rune) bool Unicode 代码点 r 在 s 中,返回 true ``` ``` ### 子串出现次数 > func Count(s, sep string) int ``` s := "helloworld" fmt.Println(strings.Count(s,"o")) //2 ``` ### 分割字符串 >func Split(s, sep string) []string func SplitN(s, sep string, n int) []string **Split(s, sep) 和 SplitN(s, sep, -1) 等价** ``` s := "爱,我,中,华" fmt.Println(strings.Split(s,","))//[爱 我 中 华] fmt.Println(strings.SplitN(s,",",1))//[爱,我,中,华] fmt.Println(strings.SplitN(s,",",2))//[爱 我,中,华] fmt.Println(strings.SplitN(s,",",3))//[爱 我 中,华] fmt.Println(strings.SplitN(s,",",-1))//[爱 我 中 华] ``` 可以看出参数中的 n 表示返回的切片中的元素 >func SplitAfter(s, sep string) []string func SplitAfterN(s, sep string, n int) []string **SplitAfter(s, sep) 和 SplitAfterN(s, sep, -1) 等价** ``` fmt.Println(strings.SplitAfter(s,","))//[爱, 我, 中, 华] fmt.Println(strings.SplitAfterN(s,",",2))//[爱, 我,中,华] ``` 可以看出,带after的函数,在生成切片后,仍然保留了step, ### 切片->字符串 ``` ss := []string{ "爱", "我", "中", "华", } fmt.Println(strings.Join(ss,"&"))//爱&我&中&华 ``` ### 前缀 ``` s := "helloworld" fmt.Println(strings.HasPrefix(s, "go"))//false fmt.Println(strings.HasPrefix(s, "he"))//true ``` ### 后缀 ``` s := "helloworld" fmt.Println(strings.HasSuffix(s, "ha")) //false fmt.Println(strings.HasSuffix(s, "ld")) //true ``` ### 字符或子字符串在字符串中出现的位置 ``` s := "helloworld" fmt.Println(strings.Index(s, "l"))//2 fmt.Println(strings.LastIndex(s, "l"))//8 ``` ### 大小写 >func ToLower(s string) string 字符全部转换为小写 > func ToUpper(s string) string 字符全部转换为大写 ``` fmt.Println(strings.ToLower("GO")) //go fmt.Println(strings.ToUpper("go")) //GO ``` ### 标题处理 >func Title(s string) string 首字母大写 >func ToTitle(s string) string 将每个字母大写 >func ToTitleSpecial(c unicode.SpecialCase, s string) string 每个字母大写,并且会将一些特殊字母转换为其对应的特殊大写字母 ``` title := "hello world" fmt.Println(strings.Title(title)) //Hello World fmt.Println(strings.ToTitle(title)) //HELLO WORLD ``` ### 字符串替换 >func Replace(s, old, new string, n int) string 用 new 替换 s 中的 old,一共替换 n 个。 如果 n < 0,则不限制替换次数,即全部替换 >func ReplaceAll(s, old, new string) string 该函数内部直接调用了函数 Replace(s, old, new , -1) ``` s := "book book book" fmt.Println(strings.Replace(s,"o", "l",1))//blok book book fmt.Println(strings.Replace(s,"o", "l",2))//bllk book book fmt.Println(strings.ReplaceAll(s,"o", "l"))//bllk bllk bllk ``` ### 重复字符串 > func Repeat(s string, count int) string 将字符串重复count次 ``` s := "book" fmt.Println(strings.Repeat(s,2))//bookbook ``` ### 裁剪字符串 > func Trim(s string, cutset string) string 将 s 左右两侧中匹配 cutset 中的任一字符的字符去掉 ``` s := "dhello world" s1 := strings.Trim(s,"d") fmt.Printf("%s",s1)//hello worl ``` >func TrimLeft(s string, cutset string) string 将 s 左侧的匹配 cutset 中的任一字符的字符去掉 ``` s := "dhello world" s1 := strings.TrimLeft(s,"d") fmt.Printf("%s",s1)//hello world ``` >func TrimRight(s string, cutset string) string 将 s 右侧的匹配 cutset 中的任一字符的字符去掉 ``` s := "dhello world" s1 := strings.TrimRight(s,"d") fmt.Printf("%s",s1)//dhello worl ``` >func TrimPrefix(s, prefix string) string 如果 s 的前缀为 prefix 则返回去掉前缀后的 string , 否则 s 没有变化。 ``` s := "dhello world" s1 := strings.TrimPrefix(s,"d") fmt.Printf("%s",s1)//hello world ``` >func TrimSuffix(s, suffix string) string 如果 s 的后缀为 suffix 则返回去掉后缀后的 string , 否则 s 没有变化。 ``` s := "dhello world" s1 := strings.TrimSuffix(s,"d") fmt.Printf("%s",s1)//dhello worl ``` >func TrimSpace(s string) string 将 s 左右两侧的间隔符去掉。常见间隔符包括:'\t', '\n', '\v', '\f', '\r', ' ' ``` s := " dhello world " s1 := strings.Trim(s," ") s2 := strings.TrimSpace(s) fmt.Printf("%s %v \n",s1,strings.Split(s1," "))//dhello world [dhello world] fmt.Printf("%s %v \n",s2,strings.Split(s2," "))//dhello world [dhello world] Trim 也可以达到TrimSpace的效果 ``` ## strconv ### Atoi ``` i,_ := strconv.Atoi("100") fmt.Printf("%d %T",i,i)//100 int ``` ### Itoa ``` s:= strconv.Itoa(100) fmt.Printf("%s %T",s,s)//"100" string ``` ### 字符串转其他类型 Parse系列函数 #### ParseBool >func ParseBool(str string) (value bool, err error) 返回字符串表示的bool值。它接受1、0、t、f、T、F、true、false、True、False、TRUE、FALSE;否则返回错误。 ``` b,_ := strconv.ParseBool("1") fmt.Printf("%T %t\n",b,b)//bool true ``` #### ParseInt >func ParseInt(s string, base int, bitSize int) (i int64, err error) base指定进制(2到36),如果base为0,则会从字符串前置判断,“0x"是16进制,“0"是8进制,否则是10进制; bitSize指定结果必须能无溢出赋值的整数类型,0、8、16、32、64 分别代表 int、int8、int16、int32、int64; ``` i, _ := strconv.ParseInt("-2", 10, 64) fmt.Printf("%T %d\n",i,i)//int64 -2 ``` #### ParseFloat >func ParseFloat(s string, bitSize int) (f float64, err error) bitSize指定了期望的接收类型,32是float32(返回值可以不改变精确值的赋值给float32),64是float64; 返回值err是*NumErr类型的,语法有误的,err.Error=ErrSyntax;结果超出表示范围的,返回值f为±Inf,err.Error= ErrRange。 ``` f, _ := strconv.ParseFloat("3.1415", 32) fmt.Printf("%T\n",f)//float64 ``` #### ParseUint >func ParseUint(s string, base int, bitSize int) (n uint64, err error) ``` u, _ := strconv.ParseUint("2", 10, 64) fmt.Printf("%T %d\n",u,u)//uint64 2 ``` ### 其他类型转字符串 Format系列函数 #### FormatBool >func FormatBool(b bool) string ``` b1 := true b2 := false fmt.Printf("%s %T \n",strconv.FormatBool(b1),strconv.FormatBool(b1))//true string fmt.Printf("%s %T \n",strconv.FormatBool(b2),strconv.FormatBool(b2))//false string ``` #### FormatInt >func FormatInt(i int64, base int) string 返回i的base进制的字符串表示。base 必须在2到36之间,结果中会使用小写字母’a’到’z’表示大于10的数字。 ``` i := int64(-100) fmt.Printf("%s %T \n",strconv.FormatInt(i,2),strconv.FormatInt(i,2))//-1100100 string ``` #### FormatFloat >func FormatFloat(f float64, fmt byte, prec, bitSize int) string * fmt表示格式:‘f’(-ddd.dddd)、‘b’(-ddddp±ddd,指数为二进制)、’e’(-d.dddde±dd,十进制指数)、‘E’(-d.ddddE±dd,十进制指数)、‘g’(指数很大时用’e’格式,否则’f’格式)、‘G’(指数很大时用’E’格式,否则’f’格式)。 * prec控制精度(排除指数部分):对’f’、’e’、‘E’,它表示小数点后的数字个数;对’g’、‘G’,它控制总的数字个数。如果prec 为-1,则代表使用最少数量的、但又必需的数字来表示f。 * bitSize表示f的来源类型(32:float32、64:float64),会据此进行舍入。 ``` s := strconv.FormatFloat(1.732, 'E', -1, 64) fmt.Printf("%s %T \n",s,s) //1.732E+00 string ``` #### FormatUint >func FormatUint(i uint64, base int) string ``` i := int64(100) fmt.Printf("%s %T \n",strconv.FormatUint(i,2),strconv.FormatUint(i,2))//1100100 string ```