// 转换// 将 s 中的所有字符修改为大写(小写、标题)格式返回。
func ToUpper(s []byte) []byte
func ToLower(s []byte) []byte
func ToTitle(s []byte) []byte// 使用指定的映射表将 s 中的所有字符修改为大写(小写、标题)格式返回。
func ToUpperSpecial(_case unicode.SpecialCase, s []byte) []byte
func ToLowerSpecial(_case unicode.SpecialCase, s []byte) []byte
func ToTitleSpecial(_case unicode.SpecialCase, s []byte) []byte// 将 s 中的所有单词的首字符修改为 Title 格式返回。
// BUG: 不能很好的处理以 Unicode 标点符号分隔的单词。
func Title(s []byte) []byte------------------------------// 比较// 比较两个 []byte,nil 参数相当于空 []byte。
// a <  b 返回 -1
// a == b 返回 0
// a >  b 返回 1
func Compare(a, b []byte) int// 判断 a、b 是否相等,nil 参数相当于空 []byte。
func Equal(a, b []byte) bool// 判断 s、t 是否相似,忽略大写、小写、标题三种格式的区别。
// 参考 unicode.SimpleFold 函数。
func EqualFold(s, t []byte) bool------------------------------// 示例:EqualFold
func main() {s1 := "Φφϕ kKK"s2 := "ϕΦφ KkK"// 看看 s1 里面是什么for _, c := range s1 {fmt.Printf("%-5x", c)}fmt.Println()// 看看 s2 里面是什么for _, c := range s2 {fmt.Printf("%-5x", c)}fmt.Println()// 看看 s1 和 s2 是否相似fmt.Println(bytes.EqualFold([]byte(s1), []byte(s2)))
}// 输出结果:
// 3a6  3c6  3d5  20   6b   4b   212a
// 3d5  3a6  3c6  20   212a 6b   4b
// true------------------------------// 清理// 去掉 s 两边(左边、右边)包含在 cutset 中的字符(返回 s 的切片)
func Trim(s []byte, cutset string) []byte
func TrimLeft(s []byte, cutset string) []byte
func TrimRight(s []byte, cutset string) []byte// 去掉 s 两边(左边、右边)符合 f 要求的字符(返回 s 的切片)
func TrimFunc(s []byte, f func(r rune) bool) []byte
func TrimLeftFunc(s []byte, f func(r rune) bool) []byte
func TrimRightFunc(s []byte, f func(r rune) bool) []byte// 去掉 s 两边的空白(unicode.IsSpace)(返回 s 的切片)
func TrimSpace(s []byte) []byte// 去掉 s 的前缀 prefix(后缀 suffix)(返回 s 的切片)
func TrimPrefix(s, prefix []byte) []byte
func TrimSuffix(s, suffix []byte) []byte------------------------------// 示例
func main() {bs := [][]byte{[]byte("Hello World !"),[]byte("Hello 世界!"),[]byte("hello golang ."),}f := func(r rune) bool {return bytes.ContainsRune([]byte("!!. "), r)}for _, b := range bs {fmt.Printf("%q\n", bytes.TrimFunc(b, f))}// "Hello World"// "Hello 世界"// "Hello Golang"for _, b := range bs {fmt.Printf("%q\n", bytes.TrimPrefix(b, []byte("Hello ")))}// "World !"// "世界!"// "hello Golang ."
}------------------------------// 拆合// Split 以 sep 为分隔符将 s 切分成多个子串,结果不包含分隔符。
// 如果 sep 为空,则将 s 切分成 Unicode 字符列表。
// SplitN 可以指定切分次数 n,超出 n 的部分将不进行切分。
func Split(s, sep []byte) [][]byte
func SplitN(s, sep []byte, n int) [][]byte// 功能同 Split,只不过结果包含分隔符(在各个子串尾部)。
func SplitAfter(s, sep []byte) [][]byte
func SplitAfterN(s, sep []byte, n int) [][]byte// 以连续空白为分隔符将 s 切分成多个子串,结果不包含分隔符。
func Fields(s []byte) [][]byte// 以符合 f 的字符为分隔符将 s 切分成多个子串,结果不包含分隔符。
func FieldsFunc(s []byte, f func(rune) bool) [][]byte// 以 sep 为连接符,将子串列表 s 连接成一个字节串。
func Join(s [][]byte, sep []byte) []byte// 将子串 b 重复 count 次后返回。
func Repeat(b []byte, count int) []byte------------------------------// 示例
func main() {b := []byte("  Hello   World !  ")fmt.Printf("%q\n", bytes.Split(b, []byte{' '}))// ["" "" "Hello" "" "" "World" "!" "" ""]fmt.Printf("%q\n", bytes.Fields(b))// ["Hello" "World" "!"]f := func(r rune) bool {return bytes.ContainsRune([]byte(" !"), r)}fmt.Printf("%q\n", bytes.FieldsFunc(b, f))// ["Hello" "World"]
}------------------------------// 子串// 判断 s 是否有前缀 prefix(后缀 suffix)
func HasPrefix(s, prefix []byte) bool
func HasSuffix(s, suffix []byte) bool// 判断 b 中是否包含子串 subslice(字符 r)
func Contains(b, subslice []byte) bool
func ContainsRune(b []byte, r rune) bool// 判断 b 中是否包含 chars 中的任何一个字符
func ContainsAny(b []byte, chars string) bool// 查找子串 sep(字节 c、字符 r)在 s 中第一次出现的位置,找不到则返回 -1。
func Index(s, sep []byte) int
func IndexByte(s []byte, c byte) int
func IndexRune(s []byte, r rune) int// 查找 chars 中的任何一个字符在 s 中第一次出现的位置,找不到则返回 -1。
func IndexAny(s []byte, chars string) int// 查找符合 f 的字符在 s 中第一次出现的位置,找不到则返回 -1。
func IndexFunc(s []byte, f func(r rune) bool) int// 功能同上,只不过查找最后一次出现的位置。
func LastIndex(s, sep []byte) int
func LastIndexByte(s []byte, c byte) int
func LastIndexAny(s []byte, chars string) int
func LastIndexFunc(s []byte, f func(r rune) bool) int// 获取 sep 在 s 中出现的次数(sep 不能重叠)。
func Count(s, sep []byte) int------------------------------// 替换// 将 s 中前 n 个 old 替换为 new,n < 0 则替换全部。
func Replace(s, old, new []byte, n int) []byte// 将 s 中的字符替换为 mapping(r) 的返回值,
// 如果 mapping 返回负值,则丢弃该字符。
func Map(mapping func(r rune) rune, s []byte) []byte// 将 s 转换为 []rune 类型返回
func Runes(s []byte) []rune------------------------------------------------------------type Reader struct { ... }// 将 b 包装成 bytes.Reader 对象。
func NewReader(b []byte) *Reader// bytes.Reader 实现了如下接口:
// io.ReadSeeker
// io.ReaderAt
// io.WriterTo
// io.ByteScanner
// io.RuneScanner// 返回未读取部分的数据长度
func (r *Reader) Len() int// 返回底层数据的总长度,方便 ReadAt 使用,返回值永远不变。
func (r *Reader) Size() int64// 将底层数据切换为 b,同时复位所有标记(读取位置等信息)。
func (r *Reader) Reset(b []byte)------------------------------// 示例
func main() {b1 := []byte("Hello World!")b2 := []byte("Hello 世界!")buf := make([]byte, 6)rd := bytes.NewReader(b1)rd.Read(buf)fmt.Printf("%q\n", buf) // "Hello "rd.Read(buf)fmt.Printf("%q\n", buf) // "World!"rd.Reset(b2)rd.Read(buf)fmt.Printf("%q\n", buf) // "Hello "fmt.Printf("Size:%d, Len:%d\n", rd.Size(), rd.Len())// Size:15, Len:9
}------------------------------------------------------------type Buffer struct { ... }// 将 buf 包装成 bytes.Buffer 对象。
func NewBuffer(buf []byte) *Buffer// 将 s 转换为 []byte 后,包装成 bytes.Buffer 对象。
func NewBufferString(s string) *Buffer// Buffer 本身就是一个缓存(内存块),没有底层数据,缓存的容量会根据需要
// 自动调整。大多数情况下,使用 new(Buffer) 就足以初始化一个 Buffer 了。// bytes.Buffer 实现了如下接口:
// io.ReadWriter
// io.ReaderFrom
// io.WriterTo
// io.ByteWeriter
// io.ByteScanner
// io.RuneScanner// 未读取部分的数据长度
func (b *Buffer) Len() int// 缓存的容量
func (b *Buffer) Cap() int// 读取前 n 字节的数据并以切片形式返回,如果数据长度小于 n,则全部读取。
// 切片只在下一次读写操作前合法。
func (b *Buffer) Next(n int) []byte// 读取第一个 delim 及其之前的内容,返回遇到的错误(一般是 io.EOF)。
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
func (b *Buffer) ReadString(delim byte) (line string, err error)// 写入 r 的 UTF-8 编码,返回写入的字节数和 nil。
// 保留 err 是为了匹配 bufio.Writer 的 WriteRune 方法。
func (b *Buffer) WriteRune(r rune) (n int, err error)// 写入 s,返回写入的字节数和 nil。
func (b *Buffer) WriteString(s string) (n int, err error)// 引用未读取部分的数据切片(不移动读取位置)
func (b *Buffer) Bytes() []byte// 返回未读取部分的数据字符串(不移动读取位置)
func (b *Buffer) String() string// 自动增加缓存容量,以保证有 n 字节的剩余空间。
// 如果 n 小于 0 或无法增加容量则会 panic。
func (b *Buffer) Grow(n int)// 将数据长度截短到 n 字节,如果 n 小于 0 或大于 Cap 则 panic。
func (b *Buffer) Truncate(n int)// 重设缓冲区,清空所有数据(包括初始内容)。
func (b *Buffer) Reset()------------------------------// 示例
func main() {rd := bytes.NewBufferString("Hello World!")buf := make([]byte, 6)// 获取数据切片b := rd.Bytes()// 读出一部分数据,看看切片有没有变化rd.Read(buf)fmt.Printf("%s\n", rd.String()) // World!fmt.Printf("%s\n\n", b)         // Hello World!// 写入一部分数据,看看切片有没有变化rd.Write([]byte("abcdefg"))fmt.Printf("%s\n", rd.String()) // World!abcdefgfmt.Printf("%s\n\n", b)         // Hello World!// 再读出一部分数据,看看切片有没有变化rd.Read(buf)fmt.Printf("%s\n", rd.String()) // abcdefgfmt.Printf("%s\n", b)           // Hello World!
}------------------------------------------------------------

多个[]byte数组合并成一个[]byte

场景:在开发中,要将多个[]byte数组合并成一个[]byte,初步实现思路如下:

1、获取多个[]byte长度

2、构造一个二维码数组

3、循环将[]byte拷贝到二维数组中

package gstoreimport ("bytes"
)//BytesCombine 多个[]byte数组合并成一个[]byte
func BytesCombine(pBytes ...[]byte) []byte {len := len(pBytes)s := make([][]byte, len)for index := 0; index < len; index++ {s[index] = pBytes[index]}sep := []byte("")return bytes.Join(s, sep)
}

结合bytes的特性,可使用join函数进行合并,如下:

package gstoreimport ("bytes"
)//BytesCombine 多个[]byte数组合并成一个[]byte
func BytesCombine(pBytes ...[]byte) []byte {return bytes.Join(pBytes, []byte(""))
}

简直酷毙了吧~~~~~~~~~~要了解语言特性,并进行重构。这就是成功的要素。

golang bytes包 []byte 字节切片 简介相关推荐

  1. Go语言学习(十)bytes包处理字节切片

    bytes包提供了对字节切片进行读写操作的一系列函数  字节切片处理的函数比较多,分为基本处理函数,比较函数,后缀检查函数,索引函数,分割函数,  大小写处理函数和子切片处理函数等. 1.字节切片基本 ...

  2. golang fmt包Printf 格式化输出 简介

    Go语言的标准输出流在打印到屏幕时有些参数跟别的语言(比如C#和Java)不同,下面是我整理的一些常用的格式化输入操作. General %v 以默认的方式打印变量的值 %T 打印变量的类型 Inte ...

  3. golang bytes.buffer 字节缓冲器 简介

    目录 创建 Buffer缓冲器 NewBuffer NewBufferString 向 Buffer 中写入数据 Write WriteString WriteByte WriteRune 完整示例 ...

  4. golang语言中bytes包的常用函数,Reader和Buffer的使用

    bytes中常用函数的使用: package main;import ("bytes""fmt""unicode" )//bytes包中实现 ...

  5. golang 字节切片 数组 字符串 互转

    目录 基本认知 将字符串转为字节切片 将字节切片转为字符串 性能 基本认知 在字符串和字节切片(数组)之间进行转换时,会得到一个全新的切片,其中包含与字符串相同的字节,反之亦然. 转换不会修改数据 唯 ...

  6. Golang bytes.Buffer 用法精述

    1.简介 bytes.Buffer 是 Golang 标准库中的缓冲区,具有读写方法和可变大小的字节存储功能.缓冲区的零值是一个待使用的空缓冲区.定义如下: type Buffer struct {b ...

  7. Golang bytes源码分析

    bytes/bytes.go源码分析 Golang JDK 1.10.3 bytes包提供了操作[]byte的常用方法. 源码分析 func equalPortable(a, b []byte) bo ...

  8. golang strings包使用

    分析 strings标准库包主要涉及字符串的基本操作. 常见字符串的操作有: 字符串求长度 求子串 是否存在某个字符或者子串 子串出现的次数(字符串匹配) 字符串分割(切分)成[]string 字符串 ...

  9. golang fmt包中的占位符

    原文地址:https://www.cnblogs.com/qing123/articles/4353353.html golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 s ...

最新文章

  1. 第五节13读取配置中的连接字符串
  2. RHEL5系统 sendmail+qpopper 架设简易邮件服务器
  3. java swing控件大全_java swing 组件大全(新手快进)
  4. 关于a标签的 href 与 onlick
  5. 百练OJ:2713:肿瘤面积
  6. 谷歌浏览器有哪些好看的主题_Kibou 简洁的Typecho主题
  7. “华为鸿蒙”操作系统下月发布?华为辟谣:请以官方声明为准
  8. 几种机器学习平台的对比和选择
  9. jQuery幸运大转盘_jQuery+PHP抽奖程序
  10. 解决datalist中单选按钮可以多选的问题(Asp.Net)
  11. 遵循PSR-4的自动加载
  12. JS Jquery 中 的遍历
  13. Opencv笔记(二十一)——傅里叶变换
  14. tgp dnf服务器文件在哪,DNFTGP补丁使用说明及问题解决方案
  15. Java从入门到放弃系列
  16. QQ坦白说使用Fiddler抓包获取json
  17. ESP32CAM摄像头图像实时传输
  18. html5 canvas api w3c官方中文,HTML5 Canvas 简介
  19. 打造国内专业企业研发管理解决方案,ONES完成华创资本领投A+轮600万美元融资
  20. Java - 50以内所有质数 (带标签的continue)

热门文章

  1. SCVMM2012R2 WinRM错误
  2. 【jQuery 区别】.click()和$(document).on(click,指定的元素,function(){});的区别
  3. 数据库访问类(使用存储过程的)
  4. View Horizon Mirage安装手册(二)——Mirage规划部署
  5. ActiveReports 报表应用教程 (2)---清单类报表
  6. 重装WIN7之后使用Ubuntu LiveCD修复grub2双系统引导
  7. 第 4 章 Glance - 022 - 如何使用 OpenStack CLI
  8. “证券教父”阚治东旗下东方汇富成失信被执行人 官方澄清
  9. jquery中对小数进行取整
  10. 先给自己定个小目标,比如写个爬虫程序