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。
?
ContainsAny用于判断子串中是否具有一个字符在源串s中。子串为空,返回false。
?
ContainsRune用于判断Ascall码代表的字符是否在源串s中。
?
Count
func Count(s, substr string) int
判断子串在源串中的数量,如果子串为空,则长度为源串的长度+1。
?
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作为分割符号。
?
HasPrefix 和 HasSuffix
func HasPrefix(s, prefix string) bool
func HasSuffix(s, suffix string) bool
判断字符串是否是以某个子串作为开头或者结尾。
?
Join
func Join(elems []string, sep string) string
使用某个sep,连接字符串。
?
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。
?
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函数中的操作。
?
Repeat
func Repeat(s string, count int) string
重复一下s,count是重复的次数,不能传负数。
?
Replace和ReplaceAll
func Replace(s, old, new string, n int) string
func ReplaceAll(s, old, new string) string
使用new来替换old,替换的次数为n。如果n为负数,则替换所有的满足条件的子串。
?
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
?
对于SplitN和SplitAfterN的第二个n说明。
?
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
?
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。
?
输出结果:
3...2...1...ignition
?
使用strings.Reader
?
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一样。
?
Len,Size,Read
Len作用: 返回未读的字符串长度。
Size的作用:返回字符串的长度。
Read的作用: 读取字符串信息,读取之后会改变Len的返回值
?
ReadAt
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
读取偏移off字节后的剩余信息到b中,ReadAt函数不会影响Len的数值。
?
ReadByte,UnreadByte
func (r *Reader) ReadByte() (byte, error)
func (r *Reader) UnreadByte() error
ReadByte从当前已读取位置继续读取一个字节。
UnreadByte将当前已读取位置回退一位,当前位置的字节标记成未读取字节。
ReadByte和UnreadByte会改变reader对象的长度。
?
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()的长度。
?
到此这篇关于Go中strings的常用方法详解的文章就介绍到这了,更多相关Go strings内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://juejin.im/post/5e5b11d4e51d4526ec0d3c1d