golang基础学习-strings包常用函数学习
Golang基础学习-strings包的学习. 1.strings包常用函数简介func len():返回字符串长度,不是strings包中函数 以下重点介绍几个常用的strings包中的函数: func Count:计算字符串包含指定字符的数量,返回int func Trim:去除左右所要过滤的字符,第一个参数字符串,第二个参数要过滤的字符 2.代码var testStr string testStr = "seetatech" testStrTrim := " seeta tech " checkStr := "tech" fmt.Println("------:" + testStr + ":-----") 2.1 Contains 介绍fmt.Println(strings.Contains(testStr,checkStr)) //验证是否包含 输出: true 2.2 Count 介绍fmt.Println(strings.Count(testStr,checkStr)) //计算字符串包含某字符数量 输出: 1 2.3 Split 介绍splitStr := strings.Split(testStr,"t") // 字符串分割 fmt.Println(splitStr) 输出: [see a ech] 2.4 Join 介绍fmt.Println(strings.Join(splitStr,"t"))// 字符串连接 输出: seetatech 2.5 Replace 介绍fmt.Println(strings.Replace(testStr,"seeta","firevison",1)) //字符串替换 输出: firevisontech 2.6 ToLower 介绍fmt.Println(strings.ToLower(testStr)) //全部小写 输出:seetatech 2.7 ToUpper 介绍fmt.Println(strings.ToUpper(testStr)) //全部大写 输出:SEETATECH 2.8 Trim 介绍fmt.Println(strings.Trim(testStrTrim," ")) //去除左右所要过滤的字符 输出:seeta tech 2.9 TrimLeft 介绍fmt.Println(strings.TrimLeft(testStrTrim," ")) //去除左所要过滤的字符 输出:seeta tech 2.10 TrimRight 介绍fmt.Println(strings.TrimRight(testStrTrim," ")) //去除右所要过滤的字符 输出: seeta tech 2.11 Index 介绍fmt.Println(strings.Index("go gopher","go")) //字符出现的位置 输出:0 2.12 LastIndex 介绍fmt.Println(strings.LastIndex("go gopher","go")) // 最后一次出现的位置 输出:3 fmt.Println(strings.LastIndex("go gopher","rodent")) //没有定位字符时,显示-1 3.学习资料strings包 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |