Golang String 包学习
博客专区 > XJ_HAHA 的博客 > 博客详情
Golang String 包学习
XJ_HAHA 发表于3年前
Golang String 包学习
  • 发表于 3年前
  • 阅读 17
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: Go string函数总结

strings 是一个很常见的类型,这里稍微总结一下一些常用函数,以备参考。

1、strings.Split 、strings.SplitAfter()、strings.SplitAfterN(), strings.SplitN(s,t,n)

strings.Split~~strings.SplitN(s,t,n)

strings.SplitAfter()、strings.SplitAfterN()

names:="Nich~Noel~Geo~Tur"

for _,name:=range strings.Split(names,"~"){

    fmt.Printf("%s|",name)

}

fmt.Println()


结果:

stringsSplit()----------------->Nich|Noel|Geo|Tur

stringsSplitAfter()------------->Nich~|Noel~|Geo~|Tur

stringsSplitAfterN(string,"sep",n)------------>n is the numbers of total substr.What is to say ,if n==1  return Nich~Noel~Geo~Tur n=2  return Nich~|Noel~Geo~Tur~ n=3 return  Nich~|Noel~|Geo~Tur~      


2、strings.FieldsFunc() 可以对两个或更多个字符进行切分

for _,name := range []string{"ad*d*c*d","Es\tsd\tsf","ma|sds|fwe23"}{

                fmt.Println(strings.FieldsFunc(name,func(char rune)bool{

                        switch char {

                        case '\t','*','|':

                                return true

                        }

                        return false

                }))

        }

3、strings.Replace

names:=strings.Replace(names,"\t"," ",n):n是要替换的次数,n=-1意思是没有限制,全部替换

Others

4、strings.Contains(s,t)if t in s return true

5. strings.Count(s,t) counts of t appear in s

6. strings.Fields(s) split by white space

13. strings.Join(xs,t) join total strings of xs by t

7. strings.HasPrefix(s,t) if s is the header of t return true

8. strings.HasSuffix(s,t) if s is the end of t return true

17. strings.NewReader(s) return the string s,s have the method of Read, ReadByte and ReadRune

18. strings.NewReplacer(...)

19. strings.Repeat(s,i)    repeat s with times i

30.strings.Map(mf,t)replace string t by the function rule  func(rune)rune


Index

9. strings.Index(s,t) return the site that t first appears in s

10. strings.IndexAny(s,t) t,s are both strings and they share some common word, return the index position that the word of t first appears in s

11. strings.IndexFunc(s,f) return index that the word in s first making f returning true

12. strings.IndexRune(s,char) return the index that char first appears in s

14. strings.LastIndex(s,t) return the last index that t appears in s

15. strings.LastIndexAny(s,t) return the index that the word of t last appears in s 

16. strings.LastIndexFunc(s,t) return the index that the word in t last making f returning true


Upper and Lowwer

20. strings.Title(s) return a new strings s that make the first letter of word in old strings Upper

21. strings.ToLower(s)return a new strings s lowwer

22. string.ToUpper(s)  return a new strings s Upper

Trim

23. strings.Trim(s,t) return a new strings which filt t from the left and right side of s

24. strings.TrimLeft(s,t) return a new strings which filt t from the left of s

25. strings.TrimRight(s,t) return a new strings which filt t from the right of s

26. strings.TrimFunc(s,f) return a new strings which the word of s make function f returning true from the left and right side of s

27. strings.TrimLeftFunc(s,f) return a new strings which the word of s make function f returning true from the left side of s

28. strings.TrimRightFunc(s,f) return a new strings which the word of s make function f returning true from the right side of s

29. strings.TrimSpace(s) return a new strings which filt space from the right of s

s:="hjlshjlkhj" if 23 ->return lshjlk  24 -> return lshjlkhj 25 -> return hjlshjlk

t:="hj"





















标签: Gopher String
共有 人打赏支持
粉丝 0
博文 4
码字总数 1725
×
XJ_HAHA
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: