Go中strings的常用方法
- 1. Compare
- 2. Contains,ContainsAny 和 ContainsRune
- 3. Count
- 4. EqualFold
- 5. Fields
- 6. HasPrefix 和 HasSuffix
- 7. Join
- 8. Index,IndexAny,IndexByte,IndexFunc,IndexRune
- 9. LastIndex,LastIndexAny,LastIndexByte和LastIndexFunc
- 10. Map
- 11. Repeat
- 12. Replace和ReplaceAll
- 13. Split,SplitN,SplitAfter和SplitAfterN
- 14. Trim,TrimFunc,TrimLeft,TrimLeftFunc,TrimPrefix,TrimSuffix,TrimRight,TrimRightFunc
- 15. 使用strings.Builder操作
- 16. 使用strings.Reader
- 17. Len,Size,Read
- 18. ReadAt
- 19. ReadByte,UnreadByte
- 20. Seek
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。
1 | fmt.Println(strings.Contains("seafood", "foo")) // true |
ContainsAny用于判断子串中是否具有一个字符在源串s中。子串为空,返回false。
1 | fmt.Println(strings.ContainsAny("team", "i")) // false |
ContainsRune用于判断Ascall码代表的字符是否在源串s中。
1 | // Finds whether a string contains a particular Unicode code point. |
Count
- func Count(s, substr string) int
判断子串在源串中的数量,如果子串为空,则长度为源串的长度+1。
1 | fmt.Println(strings.Count("cheese", "e")) // 3 |
EqualFold
- 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作为分割符号。
1 | fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) // ["foo" "bar" "baz"] |
HasPrefix 和 HasSuffix
- func HasPrefix(s, prefix string) bool
- func HasSuffix(s, suffix string) bool
判断字符串是否是以某个子串作为开头或者结尾。
1 | fmt.Println(strings.HasPrefix("Gopher", "Go")) // true |
Join
func Join(elems []string, sep string) string
使用某个sep,连接字符串。
1 | s := []string{"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。
1 | fmt.Println(strings.Index("chicken", "ken")) // 4 |
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函数中的操作。
1 | rot13 := func(r rune) rune { // r是遍历的每一个字符 |
Repeat
- func Repeat(s string, count int) string
重复一下s,count是重复的次数,不能传负数。
1 | 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为负数,则替换所有的满足条件的子串。
1 | fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) // oinky oinkky oink |
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
1 | fmt.Printf("%q\n", strings.Split("a,b,c", ",")) // ["a","b","c"] |
对于SplitN和SplitAfterN的第二个n说明。
1 | n > 0: at most n substrings; the last substring will be the unsplit remainder. |
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
1 | // Trim 包含在cutset中的元素都会被去掉 |
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。
1 | var b strings.Builder |
输出结果:
1 | 3...2...1...ignition |
strings.Builder作为字符串拼接的利器,建议加大使用力度。
1 | func (b *Builder) Cap() int // 容量,涉及批量内存分配机制 |
使用strings.Reader
1 | type Reader struct { |
A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, io.ByteScanner, and io.RuneScanner interfaces by reading from a string. The zero value for Reader operates like a Reader of an empty string.
Reader通过读取字符串的方式,实现了接口io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, io.ByteScanner和io.RuneScanner。零值Reader操作起来就像操作空字符串的io.Reader一样。
1 | func NewReader(s string) *Reader // 初始化reader实例 |
Len,Size,Read
Len作用: 返回未读的字符串长度。
Size的作用:返回字符串的长度。
Read的作用: 读取字符串信息,读取之后会改变Len的返回值
1 | r := strings.NewReader("abcdefghijklmn") |
ReadAt
- func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
读取偏移off字节后的剩余信息到b中,ReadAt函数不会影响Len的数值。
1 | r := strings.NewReader("abcdefghijklmn") |
ReadByte,UnreadByte
- func (r *Reader) ReadByte() (byte, error)
- func (r *Reader) UnreadByte() error
ReadByte从当前已读取位置继续读取一个字节。
UnreadByte将当前已读取位置回退一位,当前位置的字节标记成未读取字节。
ReadByte和UnreadByte会改变reader对象的长度。
1 | r := strings.NewReader("abcdefghijklmn") |
Seek
- func (r *Reader) Seek(offset int64, whence int) (int64, error)
ReadAt方法并不会改变Len()的值,Seek的移位操作可以改变。offset是偏移的位置,whence是偏移起始位置,支持三种位置:io.SeekStart起始位,io.SeekCurrent当前位,io.SeekEnd末位。
offset可以是负数,当时偏移起始位与offset相加得到的值不能小于0或者大于size()的长度。
1 | r := strings.NewReader("abcdefghijklmn") |