Golang 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"





















本文来自:开源中国博客

感谢作者:XJ_HAHA

查看原文:Golang String 包学习

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。