strings包

strings包的使用举例:

package main
import s "strings"
import "fmt" var p = fmt.Println func main() {
p("Contains: ", s.Contains("test", "es"))
p("Count: ", s.Count("test", "t"))
p("HasPrefix: ", s.HasPrefix("test", "te"))
p("HasSuffix: ", s.HasSuffix("test", "st"))
p("Index: ", s.Index("test", "e"))
p("Join: ", s.Join([]string{"a", "b"}, "-"))
p("Repeat: ", s.Repeat("a", ))
p("Replace: ", s.Replace("foo", "o", "", -))
p("Replace: ", s.Replace("foo", "o", "", ))
p("Split: ", s.Split("a-b-c-d-e", "-"))
p("ToLower: ", s.ToLower("TEST"))
p("ToUpper: ", s.ToUpper("test"))
p()
p("Len: ", len("hello"))
p("Char:", "hello"[])
}

bytes包

1、大小写转换

func ToUpper(s []byte) []byte { return Map(unicode.ToUpper, s) }
func ToLower(s []byte) []byte { return Map(unicode.ToLower, s) }
func ToTitle(s []byte) []byte { return Map(unicode.ToTitle, s) }

2、比较

func Compare(a, b []byte) int
func Equal(a, b []byte) bool
func EqualFold(s, t []byte) bool

3、替换

// 将 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

4、清除

// 去掉 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

5、分割、连接

// 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

6、子串

// 判断 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

7、

func NewReader(b []byte) *Reader
  • NewReader创建一个从s读取数据的Reader。

buffer包

bytes.buffer是一个缓冲byte类型的缓冲器,这个缓冲器里存放着都是byte。

Buffer结构体定义如下:

type Buffer struct {
buf []byte // contents are the bytes buf[off : len(buf)]
off int // read at &buf[off], write at &buf[len(buf)]
bootstrap []byte // memory to hold first slice; helps small buffers avoid allocation.
lastRead readOp // last read operation, so that Unread* can work correctly.
}

buffer上的操作:

1、初始化buffer

func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
func NewBufferString(s string) *Buffer { return &Buffer{buf: []byte(s)} }
  • NewBuffer方法将 buf 包装成 bytes.Buffer 对象;
  • NewBufferString方法将string转换为byte之后,包装成bytes.Buffer对象;

2、读buffer

func (b *Buffer) Read(p []byte) (n int, err error)
func (b *Buffer) ReadByte() (c byte, err error)
func (b *Buffer) ReadRune() (r rune, size int, err error)
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
func (b *Buffer) ReadString(delim byte) (line string, err error)
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
func (b *Buffer) Next(n int) []byte
  • Read方法将缓存器buf[b.off:]的内容读到参数p中,缓冲器相应的减少了,返回的n为成功读的数量;
  • ReadByte方法返回一个字节;
  • ReadRune方法定义了如何读取Buffer中UTF8编码的rune数据;
  • ReadBytes和ReadString方法读取Buffer中从off到第一次delim之间的数据,并且包括delim;
  • Next方法读取前 n 字节的数据并以切片形式返回,如果数据长度小于 n,则全部读取。

3、写buffer

func (b *Buffer) Write(p []byte) (n int, err error)
func (b *Buffer) WriteString(s string) (n int, err error)
func (b *Buffer) WriteByte(c byte) error
func (b *Buffer) WriteRune(r rune) (n int, err error)
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
  • 使用Write方法,将一个byte类型的slice放到缓冲器的尾部;
  • 使用WriteString方法,将一个字符串放到缓冲器的尾部;
  • 使用WriteByte方法,将一个byte类型的数据放到缓冲器的尾部;
  • 使用WriteRune方法,将一个rune类型的数据放到缓冲器的尾部;
  • 使用WriteTo方法,将一个缓冲器的数据写到w里,w是实现io.Writer的,比如os.File就是实现io.Writer;

binary包

func Read(r io.Reader, order ByteOrder, data interface{}) error
func Write(w io.Writer, order ByteOrder, data interface{}) error
  • Read方法从r中读取binary编码的数据并赋给data,data必须是一个指向定长值的指针或者定长值的切片。从r读取的字节使用order指定的字节序解码并写入data的字段里当写入结构体是,名字中有'_'的字段会被跳过,这些字段可用于填充(内存空间)。
  • Write方法将data的binary编码格式写入w,data必须是定长值、定长值的切片、定长值的指针。order指定写入数据的字节序,写入结构体时,名字中有'_'的字段会置为0。

GoLang之strings、buffers、bytes、binary包的更多相关文章

  1. 19-03【golang】strings包

    golang的strings包提供了字符串操作的一系列函数.下面做个简单介绍 函数 用法 备注 Compare(a,b sring) 比较两个字符串   Contains(s, substr stri ...

  2. golang学习笔记15 golang用strings.Split切割字符串

    golang用strings.Split切割字符串 kv := strings.Split(authString, " ") if len(kv) != 2 || kv[0] != ...

  3. python读取文件报错:pandas.errors.ParserError: iterator should return strings, not bytes (did you open the file in text mode?)

    python 读取csv文件报错问题 import csv with open('E:/Selenium2script/DDT模块/test.csv','rb') as f: readers = cs ...

  4. Golang学习 - strings 包

    ------------------------------------------------------------ strings 包与 bytes 包中的函数用法基本一样,不再赘述. 只对 R ...

  5. Golang之strings包

    只列举了部分函数方法的使用: 太多了....... package main import ( "fmt" "strings" ) func main() { ...

  6. golang——(strings包)常用字符串操作函数

    (1)func HasPrefix(s, prefix string) bool 判断字符串s是否有前缀字符串prefix: (2)func HasSuffix(s, suffix string) b ...

  7. Golang 绘图技术(image/draw包介绍)

          image/draw 包仅仅定义了一个操作:通过可选的蒙版图(mask image),把一个原始图片绘制到目标图片上,这个操作是出奇的灵活,可以优雅和高效的执行很多常见的图像处理任务. 1 ...

  8. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  9. golang项目git-subtree完美解决差异包管理

    目标: 1.把golang官方已移动过url的包跟随自己的项目git代码上传到项目源码中. 2.把或自己修改过的差异化fork包跟随自己的项目git代码上传到项目源码中. 解决方案: 方案1.第三方包 ...

随机推荐

  1. 20172302 《Java软件结构与数据结构》第五周学习总结

    2018年学习总结博客总目录:第一周 第二周 第三周 第四周 第五周 教材学习内容总结 查找 查找即在某项目组中寻找某一指定目标元素,或确定该组中并不存在此元素.对其进行查找的项目组称为查找池. 1. ...

  2. pycharm如何设置python版本、设置国内pip镜像、添加第三方类库

    直接上图(mac环境): 一.设置项目的python版本 File->Default Settings ... 在弹出的界面上(参考下图),左上角的下拉框里,选择python解释器的版本即可(建 ...

  3. mysql 时间类型精确到毫秒、微秒及其处理

    一.MySQL 获得毫秒.微秒及对毫秒.微秒的处理 MySQL 较新的版本中(MySQL 6.0.5),也还没有产生微秒的函数,now() 只能精确到秒. MySQL 中也没有存储带有毫秒.微秒的日期 ...

  4. linux -- 查看磁盘空间的大小

    Ubuntu 查看磁盘空间大小命令 df -h Df命令是linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息, 命令格式: df -hl  显示格式为:  文件系统 容量 已 ...

  5. windows Server 2008 R2 IE增强安全配置正在阻止来自下列网站的内容

    1.在windows Server 2008 R2上访问百度,会出现以下界面 当在Windows Sever 2008 R2中运动IE8的时候会发现默认情况下IE启用了增强的安全配置,为了方便而且是在 ...

  6. leetcode344 反转字符串 c++实现

    编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man, a p ...

  7. ng-repeat不添加容器标签

    如UL中的循环, 我们不期望添加额外的div之类的容器标签, 使用ng-repeat-start和ng-repeat-end可以实现 <li class="item item-icon ...

  8. Ubuntu16.04上使用Anaconda3的Python3.6的pip安装UWSGI报错解决办法

    具体报错信息: lto1: fatal error: bytecode stream generated with LTO version 6.0 instead of the expected 4. ...

  9. Andriod书籍准备

    老大说公司准备开发MFC项目,过了一段时间又说开发Andriod,好吧,我现在准备Andriod. 鬼知道过段时间会变成什么. http://pan.baidu.com/share/link?shar ...

  10. 【概念原理】四种SQL事务隔离级别和事务ACID特性

    摘要: SQL事务隔离级别和事务的ACID特性 事务是一组读写操作,并且具有只有所有操作都成功才算成功的特性.   事务隔离级别 SQL事务隔离级别由弱到强分别是:READ_UNCOMMITTED.R ...