温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

GO中常用包笔记 bytes(四)

发布时间:2020-07-13 10:32:51 来源:网络 阅读:593 作者:duanbowen 栏目:开发技术

Package bytes

对字节数组进行操作的包。功能和strings包相似.

bytes包提供的功能有:

  1. 和另一个字节数组切片的关系(逐字节比较大小,是否相等/相似,是否包含/包含次数,位置搜索,是否是前缀后缀)

2.字节数组切片和字符串的关系(字符串中是否含有字节数组所包含的rune,以及在字符串中的位置)

3.字节数组切片和rune的关系(字节数组中是否含有特定的或满足特定条件的rune,以及在字节数组中的位置)

4.字节数组切片和字节的关系(包含/位置)

5.分割分组,分组连结

6.大小写转换/标题化

7.修剪两端

8.按规则修改包含的每个rune

9.重复

10.替换

11.提供处理字节流的Buffer,可读可写(strings包没有提供)

12提供Reader


具体如下:

字节数组切片和字节数组切片的关系(逐字节比较,相等,相似,包含,出现的位置,包含次数,是否是前缀后缀)

func Compare(a, b []byte) int  //逐字节比较

func Equal(a, b []byte) bool     //是否相等

func EqualFold(s, t []byte) bool   //是否相似(只有大小写可能不同)

   func Contains(b, subslice []byte) bool //后者是否为前者子串

func Index(s, sep []byte) int            //后者在前者中出现的位置

func LastIndex(s, sep []byte) int   

func Count(s, sep []byte) int      //sep在s中的重复次数。如果sep为空,返回len([]rune(s))+1

func HasPrefix(s, prefix []byte) bool     //后者是否是前者的前缀

func HasSuffix(s, suffix []byte) bool     //后者是否是前者的后缀

 

   字符数组切片和字符串的关系(是否包含串里任何一个rune以及在字符串中的位置)

func ContainsAny(b []byte, chars string) bool //字符串中是否有指定的runes中的任何一个

func IndexAny(s []byte, chars string) int    //ContainsAny调用此方法;判断字符串中是否含有指定的runes中的任何一个;并返回rune出现在字符串中的第一个位置

func LastIndexAny(s []byte, chars string) int

 

字符数组切片和rune的关系(包含,转换)

func ContainsRune(b []byte, r rune) bool //       rune是否存在于字节数组切片中

func IndexRune(s []byte, r rune) int          //从字节切片中寻找一个rune

func IndexFunc(s []byte, f func(r rune) bool) int   //从字节切片中寻找满足条件的rune(的位置)

func LastIndexFunc(s []byte, f func(r rune) bool) int

func Runes(s []byte) []rune   //类型转换

 

字符数组切片和字节的关系(位置,)

func IndexByte(s []byte, c byte) int      //位置

func LastIndexByte(s []byte, c byte) int

 

 

 

操作字节数组切片(使用空格或者符合条件的rune分割分组,将分组连接成字符串,分割,大小写转换,修剪两端,按规则修改包含的每个rune,重复,替换)

func Fields(s []byte) [][]byte      //使用空格将字节分组

func FieldsFunc(s []byte, f func(rune) bool) [][]byte //使用特定的rune(满足条件的)将字节分隔分组

func Join(s [][]byte, sep []byte) []byte       //连接成字符串 连接符为string(sep)

func Split(s, sep []byte) [][]byte      //分割

func SplitAfter(s, sep []byte) [][]byte   //分割后分隔符跟在每组结尾

func SplitAfterN(s, sep []byte, n int) [][]byte 

func SplitN(s, sep []byte, n int) [][]byte  //指定分割次数

 

func Title(s []byte) []byte    //标题化(首字母大写)

func ToLower(s []byte) []byte

func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte

func ToTitle(s []byte) []byte   //标题化(所有大写)

func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte

func ToUpper(s []byte) []byte

func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte

func Trim(s []byte, cutset string) []byte

func TrimFunc(s []byte, f func(r rune) bool) []byte

func TrimLeft(s []byte, cutset string) []byte

func TrimLeftFunc(s []byte, f func(r rune) bool) []byte

func TrimPrefix(s, prefix []byte) []byte

func TrimRight(s []byte, cutset string) []byte

func TrimRightFunc(s []byte, f func(r rune) bool) []byte

func TrimSpace(s []byte) []byte

func TrimSuffix(s, suffix []byte) []byte

 

         

func Map(mapping func(r rune) rune, s []byte) []byte //按照指定的方法修改字节数组切片中的每个rune(返回的是副本)

func Repeat(b []byte, count int) []byte   //重复

func Replace(s, old, new []byte, n int) []byte  //替换



type Reader

    func NewReader(b []byte) *Reader

    func (r *Reader) Len() int   //未读的长度

    func (r *Reader) Read(b []byte) (n int, err error)

    func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)  //拷贝从指定位置到结束的全部字节到b中,不会改变状态

    func (r *Reader) ReadByte() (byte, error)

    func (r *Reader) ReadRune() (ch rune, size int, err error)

    func (r *Reader) Reset(b []byte) //重置位置,并用b作为数据源

    func (r *Reader) Seek(offset int64, whence int) (int64, error) //设置读取指针的位置到指定的offset,whence的取值为io.SeekStart ,io.SeekStart  ,io.SeekEnd,分别表示绝对偏移,相对文件起始位置偏移,相对文件终止位置偏移 

    func (r *Reader) Size() int64  //可读的总大小

    func (r *Reader) UnreadByte() error //撤销

    func (r *Reader) UnreadRune() error //撤销

    func (r *Reader) WriteTo(w io.Writer) (n int64, err error)


type Buffer //strings包没有类似功能

    func NewBuffer(buf []byte) *Buffer

    func NewBufferString(s string) *Buffer

    func (b *Buffer) Bytes() []byte //返回未读部分的切片

    func (b *Buffer) Cap() int     //Buffer总容量

    func (b *Buffer) Grow(n int)   //为Buffer扩充容量

    func (b *Buffer) Len() int     //未读部分的大小

    func (b *Buffer) Next(n int) []byte //连续读出一段数据,返回切片

    func (b *Buffer) Read(p []byte) (n int, err error) //读出len(p)长度的数据 而非cap(p)

    func (b *Buffer) ReadByte() (byte, error)  //读出字节

    func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)//连续读出 直到遇到delim(返回的切片包含delim)

    func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) //将io.reader作为数据源

    func (b *Buffer) ReadRune() (r rune, size int, err error)  //读一个rune,r, n := utf8.DecodeRune(b.buf[b.off:])

    func (b *Buffer) ReadString(delim byte) (line string, err error) //读出string,直到遇到delim

    func (b *Buffer) Reset() //重置,清空buffer

    func (b *Buffer) String() string  //将未读出部分作为string返回(不改变状态)

    func (b *Buffer) Truncate(n int) //除了保留n长度的未读内容 其余未读内容清空,压缩BUFFER

    func (b *Buffer) UnreadByte() error //撤销

    func (b *Buffer) UnreadRune() error

    func (b *Buffer) Write(p []byte) (n int, err error) //将p写入Buffer尾部

    func (b *Buffer) WriteByte(c byte) error  //将字节写入

    func (b *Buffer) WriteRune(r rune) (n int, err error) //将Rune写入

    func (b *Buffer) WriteString(s string) (n int, err error) //将字符串写入

    func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) //将未读部分写入io.writer 并清空Buffer



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI