您现在的位置是:网站首页> 编程资料编程资料
Go中strings的常用方法详解_Golang_
2023-05-26
434人已围观
简介 Go中strings的常用方法详解_Golang_
string操作在编程中具有极高的频率,那么string中有哪些有用的方法呢?
使用strings直接操作
Compare
- func Compare(a, b string) int
按照字典序比较两个字符串,通常情况下直接使用=,>,<会更快一些。
Contains,ContainsAny 和 ContainsRune
- func Contains(s, substr string) bool
- func ContainsAny(s, chars string) bool
- func ContainsRune(s string, r rune) bool
字符串s中是否包含substr,返回true或者false。
fmt.Println(strings.Contains("seafood", "foo")) // true fmt.Println(strings.Contains("seafood", "bar")) // false fmt.Println(strings.Contains("seafood", "")) // true fmt.Println(strings.Contains("", "")) // true ContainsAny用于判断子串中是否具有一个字符在源串s中。子串为空,返回false。
fmt.Println(strings.ContainsAny("team", "i")) // false fmt.Println(strings.ContainsAny("fail", "ui")) // true fmt.Println(strings.ContainsAny("ure", "ui")) // true fmt.Println(strings.ContainsAny("failure", "ui")) // true fmt.Println(strings.ContainsAny("foo", "")) // false fmt.Println(strings.ContainsAny("", "")) // false ContainsRune用于判断Ascall码代表的字符是否在源串s中。
// Finds whether a string contains a particular Unicode code point. // The code point for the lowercase letter "a", for example, is 97. fmt.Println(strings.ContainsRune("aardvark", 97)) fmt.Println(strings.ContainsRune("timeout", 97))Count
- func Count(s, substr string) int
判断子串在源串中的数量,如果子串为空,则长度为源串的长度+1。
fmt.Println(strings.Count("cheese", "e")) // 3 fmt.Println(strings.Count("five", "")) // before & after each rune 5=4+1EqualFold
- func EqualFold(s, t string) bool
在不区分大小写的情况下,判断两个字符串是否相同。
Fields
- func Fields(s string) []string
- func FieldsFunc(s string, f func(rune) bool) []string
Fields:使用空白分割字符串。
FieldsFunc:根据传入的函数分割字符串,如果当前参数c不是数字或者字母,返回true作为分割符号。
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) // ["foo" "bar" "baz"] f := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f)) // ["foo1" "bar2" "baz3"] HasPrefix 和 HasSuffix
- func HasPrefix(s, prefix string) bool
- func HasSuffix(s, suffix string) bool
判断字符串是否是以某个子串作为开头或者结尾。
fmt.Println(strings.HasPrefix("Gopher", "Go")) // true fmt.Println(strings.HasPrefix("Gopher", "C")) // false fmt.Println(strings.HasPrefix("Gopher", "")) // true fmt.Println(strings.HasSuffix("Amigo", "go")) // true fmt.Println(strings.HasSuffix("Amigo", "O")) // false fmt.Println(strings.HasSuffix("Amigo", "Ami")) // false fmt.Println(strings.HasSuffix("Amigo", "")) // true Join
- func Join(elems []string, sep string) string
使用某个sep,连接字符串。
s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) // foo,bar,baz Index,IndexAny,IndexByte,IndexFunc,IndexRune
- func Index(s, substr string) int
- func IndexAny(s, chars string) int
- func IndexByte(s string, c byte) int
- func IndexFunc(s string, f func(rune) bool) int
- func IndexRune(s string, r rune) int
Index,IndexAny,IndexByte,IndexFunc,IndexRune都是返回满足条件的第一个位置,如果没有满足条件的数据,返回-1。
fmt.Println(strings.Index("chicken", "ken")) // 4 fmt.Println(strings.Index("chicken", "dmr")) // -1 // 子串中的任意字符在源串出现的位置 fmt.Println(strings.IndexAny("chicken", "aeiouy")) // 2 fmt.Println(strings.IndexAny("crwth", "aeiouy")) // -1 // IndexByte,字符在字符串中出现的位置 fmt.Println(strings.IndexByte("golang", 'g')) // 0 fmt.Println(strings.IndexByte("gophers", 'h')) // 3 fmt.Println(strings.IndexByte("golang", 'x')) // -1 // IndexFunc 满足条件的作为筛选条件 f := func(c rune) bool { return unicode.Is(unicode.Han, c) } fmt.Println(strings.IndexFunc("Hello, 世界", f)) // 7 fmt.Println(strings.IndexFunc("Hello, world", f)) // -1 // 某个字符在源串中的位置 fmt.Println(strings.IndexRune("chicken", 'k')) // 4 fmt.Println(strings.IndexRune("chicken", 'd')) // -1 LastIndex,LastIndexAny,LastIndexByte和LastIndexFunc
- func LastIndex(s, substr string) int
- func LastIndexAny(s, chars string) int
- func LastIndexByte(s string, c byte) int
- func LastIndexFunc(s string, f func(rune) bool) int
LastIndex,LastIndexAny,LastIndexByte,LastIndexFunc和Index,IndexAny,IndexByte,IndexFunc,IndexRune用法保持一致,从右往前计数。
Map
- func Map(mapping func(rune) rune, s string) string
对字符串s中每一个字符执行map函数中的操作。
rot13 := func(r rune) rune { // r是遍历的每一个字符 switch { case r >= 'A' && r <= 'Z': return 'A' + (r-'A'+13)%26 case r >= 'a' && r <= 'z': return 'a' + (r-'a'+13)%26 } return r } fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) Repeat
- func Repeat(s string, count int) string
重复一下s,count是重复的次数,不能传负数。
fmt.Println("ba" + strings.Repeat("na", 2))Replace和ReplaceAll
- func Replace(s, old, new string, n int) string
- func ReplaceAll(s, old, new string) string
使用new来替换old,替换的次数为n。如果n为负数,则替换所有的满足条件的子串。
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) // oinky oinkky oink fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) moo moo moo ReplaceAll使用new替换所有的old,相当于使用Replace时n<0。
Split,SplitN,SplitAfter和SplitAfterN
- func Split(s, sep string) []string
- func SplitAfter(s, sep string) []string
- func SplitAfterN(s, sep string, n int) []string
- func SplitN(s, sep string, n int) []string
fmt.Printf("%q\n", strings.Split("a,b,c", ",")) // ["a","b","c"] fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) // ["" "man " "plan " "canal panama"] fmt.Printf("%q\n", strings.Split(" xyz ", "")) // [" " "x" "y" "z" " "] fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) // [""] // SplitN 定义返回之后的切片中包含的长度,最后一部分是未被处理的。 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) // ["a", "b,c"] z := strings.SplitN("a,b,c", ",", 0) fmt.Printf("%q (nil = %v)\n", z, z == nil) // [] (nil = true) // 使用sep分割,分割出来的字符串中包含sep,可以限定分割之后返回的长度。 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) // ["a,", "b,c"] // 完全分割 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) // ["a,","b,", "c"]对于SplitN和SplitAfterN的第二个n说明。
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
Trim,TrimFunc,TrimLeft,TrimLeftFunc,TrimPrefix,TrimSuffix,TrimRight,TrimRightFunc
- func Trim(s string, cutset string) string
- func TrimFunc(s string, f func(rune) bool) string
- func TrimLeft(s string, cutset string) string
- func TrimLeftFunc(s string, f func(rune) bool) string
- func TrimPrefix(s, prefix string) string
- func TrimSuffix(s, suffix string) string
- func TrimRight(s string, cutset string) string
- func TrimRightFunc(s string, f func(rune) bool) string
// Trim 包含在cutset中的元素都会被去掉 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) // Hello, Gophers // TrimFunc去掉满足条件的字符 fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) // TrimLeft 去掉左边满足包含在cutset中的元素,直到遇到不在cutset中的元素为止 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) // Hello, Gophers!!! // TrimLeftFunc 去掉左边属于函数返回值部分,直到遇到不在cutset中的元素为止 fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) // Hello, Gophers!!! // TrimPrefix 去掉开头部分;TrimSuffix 去掉结尾部分 var s = "¡¡¡Hello, Gophers!!!" s = strings.TrimPrefix(s, "¡¡¡Hello, ") s = strings.TrimPrefix(s, "¡¡¡Howdy, ") fmt.Print(s) TrimRight,TrimRightFunc和TrimLeft,TrimLeftFunc功能保持一直,无需赘述。
使用strings.Builder操作
A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero Builder.
strings.Builder使用Write方法来高效的构建字符串。它最小化了内存拷贝,耗费零内存,不要拷贝非零的Builder。
var b strings.Builder for i := 3; i >= 1; i-- { fmt.Fprintf(&b, "%d...", i) } b.WriteString("ignition") fmt.Println(b.String()) 输出结果:
3...2...1...ignition
strings.Builder作为字符串拼接的利器,建议加大使用力度。 func (b *Builder) Cap() int // 容量,涉及批量内存分配机制 func (b *Builder) Grow(n int) // 手动分配内存数量 func (b *Builder) Len() int // 当前builder中含有的所有字符长度 func (b *Builder) Reset() // 清空builder func (b *Builder) String() string // 转化为字符串输出 func (b *Builder) Write(p []byte) (int, error) // 往builder写入数据 func (b *Builder) WriteByte(c byte) error // 往builder写入数据 func (b *Builder) WriteRune(r rune) (int, error) // 往builder写入数据 func (b *Builder) WriteString(s string) (int
相关内容
- Go语言defer语句的三种机制整理_Golang_
- 使用go module导入本地包的方法教程详解_Golang_
- Golang使用lua脚本实现redis原子操作_Golang_
- Go语言正则表达式的使用详解_Golang_
- 基于golang的简单分布式延时队列服务的实现_Golang_
- golang 微服务之gRPC与Protobuf的使用_Golang_
- GO语言中的方法值和方法表达式的使用方法详解_Golang_
- Golang中的Slice与数组及区别详解_Golang_
- Golang import本地包和导入问题相关详解_Golang_
- golang实现微信小程序商城后台系统(moshopserver)_Golang_
点击排行
本栏推荐
