Golang String 包学习

XJ_HAHA · · 14041 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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 包学习

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

14041 次点击  
加入收藏 微博
上一篇:golang测试技术
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传