加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang讲解(go语言)标准库分析之strings

发布时间:2020-12-16 18:36:05 所属栏目:大数据 来源:网络整理
导读:来源:http://www.widuu.com/archives/01/941.html 今天我们继续哈,争取我们把strings包这个东西给弄完了,这已经也有两天没有好好更新了,所以这两天更新的比较多补充前两天的 (1)func Title(s string) string这个函数作用很简单,就是把输入的字符串首字

来源:http://www.widuu.com/archives/01/941.html


今天我们继续哈,争取我们把strings包这个东西给弄完了,这已经也有两天没有好好更新了,所以这两天更新的比较多补充前两天的
(1)func Title(s string) string这个函数作用很简单,就是把输入的字符串首字母大写,参数是字符串返回的是字符串

1 import (
2 "fmt"
3 "strings"
4 )
5
6 func main() {
7 fmt.Println(strings.Title("hello word widuu write"))
8 }

(2)func ToLower(s string) string这个函数是把字符串转换成小写,当然都是副本哦

fmt.Println(strings.ToLower("WIDUU"))//widuu
(3)func ToTitleSpecial(_case unicode.SpecialCase,s string) string该函数把s字符串里面的每个单词转化为标题体,但是调用的是unicode.SpecialCase的ToTitle方法

01
02 03 04 "unicode"
05 )
06
07 func main() {
08 var SC unicode.SpecialCase
09 fmt.Println(strings.ToTitleSpecial(SC,"Gopher"10 //GOPHER
11 (4)func ToUpper(s string) string该函数就是把所有的字符串都大写

fmt.Println(strings.ToUpper("widuu"//WIDUU
(5)func ToUpperSpecial(_case unicode.SpecialCase,s string) string该函数把s字符串里面的每个单词转化为大写,但是调用的是unicode.SpecialCase的ToUpper方法

fmt.Println(strings.ToUpperSpecial(SC,sans-serif; font-size:14px; line-height:25px"> (6)func Trim(s string,cutset string) string这个函数大家估计就看的多了,这个函数是去除两边的自定义字符的

fmt.Println(strings.Trim("&&&&&nihao&&&&",255)!important; border:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">"&"//nihao
(7)func TrimFunc(s string,f func(rune) bool) string该函数大家是不是看的多了,对就是自定义函数清楚,那我们自定义函数完成上边那个函数的效果吧

fmt.Println(strings.TrimFunc("&&&&nihao&&&&"
if r =='&' {
return true
}
false
12 }))//nihao
13 (8)func TrimLeft(s string,cutset string) string该函数是删除左边的定义的字符

fmt.Println(strings.TrimLeft("&widuu&"//widuu&
(9)func TrimLeftFunc(s string,f func(rune) bool) string这个你说我还用再多说吗?上边遇到这种类型的是不是多的吓人,写的都要吐了,好吧根据自定义函数来删除左边

fmt.Println(strings.TrimLeftFunc(// widuu&是不是跟上边的那个函数效果一样了
(10)func TrimPrefix(s,prefix string) string前边我们讲了HasPrefix判断前缀的,这个是去除前缀的,来我们see下

s :="widuu_xiaowei"
strings.HasPrefix(s,255)!important; border:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">"widuu_") {
fmt.Println(strings.TrimPrefix(s,191)!important; border:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">//xiaowei
return
}
fmt.Println(s)
14 (11)

func TrimRight(s string,cutset string) string
func TrimRightFunc(s string,f func(rune) bool) string 跟上边的left一样,只是这个是右边而已

(12)func TrimSpace(s string) string,清楚文本里边的空白操作rnt

fmt.Println(strings.TrimSpace("rtn widuu t" (13)func TrimSuffix(s,suffix string) string这个函数去除后缀的,跟TrimPrfix这个正好相反,我们看实例

查看源代码
打印 帮助
"web_widuu"
strings.HasSuffix(s,255)!important; border:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">"_widuu"fmt.Println(strings.TrimSuffix(s,191)!important; border:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; background:none!important">//web
fmt.Println(s)
13

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读