Go Bytes包

  • 大小写转换

func ToUpper(s []byte) []byte //转大写

1
bytes.ToUpper(str)

func ToLower(s []byte) []byte //转小写

1
bytes.ToLower(str)

func ToTitle(s []byte) []byte //转大写 标题格式返回

1
bytes.ToTitle(str)
1
2
3
4
5
6
7
str := []byte("hello world")
//转小写
fmt.Println(string(bytes.ToLower(str)))
//转大写
fmt.Println(string(bytes.ToUpper(str)))
//转大写 标题格式返回
fmt.Println(string(bytes.ToTitle(str)))
  • 使用指定的映射表将 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

1
2
3
4
// 使用指定的映射表将 s 中的所有字符修改为大写(小写、标题)格式返回。
fmt.Println(string(bytes.ToLowerSpecial(unicode.AzeriCase,str)))
fmt.Println(string(bytes.ToUpperSpecial(unicode.AzeriCase,str)))
fmt.Println(string(bytes.ToTitleSpecial(unicode.AzeriCase,str)))

func Title(s []byte) []byte

  • 将 s 中的所有单词的首字符修改为 Title 格式返回。

  • BUG: 不能很好的处理以 Unicode 标点符号分隔的单词。

    1
    2
    //Title 格式返回 首字母大写
    fmt.Println(string(bytes.Title(str)))
  • 比较

func Compare(a, b []byte) int

  • 比较两个 []byte,nil 参数相当于空 []byte。
  • a < b 返回 -1
  • a == b 返回 0
  • a > b 返回 1

func Equal(a, b []byte) bool

  • 判断 a、b 是否相等,nil 参数相当于空 []byte。

func EqualFold(s, t []byte) bool

  • 判断 s、t 是否相似,忽略大写、小写、标题三种格式的区别。

  • 参考 unicode.SimpleFold 函数。

  • 修剪

  • 去掉 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 Trim### func (s []byte, f ### func (r rune) bool) []byte

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

func TrimRight### func (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

  • 截取
  • 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 Fields### func (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

  • 索引查找
  • 判断 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 Index### func (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 LastIndex### func (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

  • 完整实例代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    package main

    import (

    )
    import (
    "fmt"
    "bytes"

    "unicode"
    )

    func main() {


    str := []byte("hello world")
    //转小写
    fmt.Println(string(bytes.ToLower(str)))
    //转大写
    fmt.Println(string(bytes.ToUpper(str)))
    //转大写 标题格式返回
    fmt.Println(string(bytes.ToTitle(str)))

    // 使用指定的映射表将 s 中的所有字符修改为大写(小写、标题)格式返回。
    fmt.Println(string(bytes.ToLowerSpecial(unicode.AzeriCase,str)))
    fmt.Println(string(bytes.ToUpperSpecial(unicode.AzeriCase,str)))
    fmt.Println(string(bytes.ToTitleSpecial(unicode.AzeriCase,str)))

    //Title 格式返回 首字母大写
    fmt.Println(string(bytes.Title(str)))


    fmt.Println(bytes.Compare([]byte("a"), []byte("a"))) //a == b 返回 0
    fmt.Println(bytes.Compare([]byte("a"), []byte("b"))) //a < b 返回 -1
    fmt.Println(bytes.Compare([]byte("b"), []byte("a"))) //b > a 返回 1


    //Equal 判断a、b是否相等,nil参数相当于空[]byte
    fmt.Println(bytes.Equal([]byte("a"), []byte("a"))) //true
    fmt.Println(bytes.Equal([]byte("b"), []byte("a"))) //false


    //EqualFold 判断是否相似 忽略大小写区别
    fmt.Println(bytes.EqualFold([]byte("b"), []byte("a"))) //false
    fmt.Println(bytes.EqualFold([]byte("hello world"), []byte("HELLO World"))) //true

    //修剪
    fmt.Println(string(bytes.Trim([]byte("hello world"), "hello "))) //world
    fmt.Println(string(bytes.TrimLeft([]byte("hello world"), "hello "))) //world
    fmt.Println(string(bytes.Trim([]byte("hello world"), "rld"))) //world

    s := []byte("hello world")
    fmt.Println(string(bytes.TrimFunc(s,func(r rune)bool{
    if r=='h' || r=='d'{
    return true
    }else{
    return false
    }
    }))) //ello worl

    fmt.Println(string(bytes.TrimLeftFunc(s,func(r rune)bool{
    if r=='h' || r=='d'{
    return true
    }else{
    return false
    }
    }))) //ello world

    fmt.Println(string(bytes.TrimRightFunc(s,func(r rune)bool{
    if r=='h' || r=='d'{
    return true
    }else{
    return false
    }
    }))) //hello worl

    //修剪前后空格
    str2 := []byte(" -- hello world -- ")
    fmt.Println(string(str2))
    fmt.Println(string(bytes.TrimSpace(str2)))

    //修剪前缀 后缀
    fmt.Println(string(bytes.TrimPrefix(str2,[]byte(" -- "))))
    fmt.Println(string(bytes.TrimSuffix(str2,[]byte(" -- "))))

    //拆合
    //Split 以 sep 为分隔符将 s 切分成多个子串,结果不包含分隔符
    //如果 sep 为空,则将 s 切分成 Unicode 字符列表。
    res := bytes.Split(str2, []byte(" h"))
    fmt.Println(res)
    for _,v := range res{
    fmt.Println(string(v))
    }


    //SplitN 可以指定切分次数 n,超出 n 的部分将不进行切分
    res = bytes.SplitN(str2, []byte("l"),2) //切分次数2次 超出不再切分
    fmt.Println(res)
    for _,v := range res{
    fmt.Println(string(v))
    }




    str2 = []byte(" -- hello world -- ")

    // 功能同 Split,只不过结果包含分隔符(在各个子串尾部)
    res = bytes.SplitAfter(str2, []byte("l"))
    fmt.Println(res)
    for _,v := range res{
    fmt.Println(string(v))
    }


    res = bytes.SplitAfterN(str2, []byte("l"),2) //限定切分次数 到达次数不再切分
    fmt.Println(res)
    for _,v := range res{
    fmt.Println(string(v))
    }



    //以连续空白为分隔符将 s 切分成多个子串,结果不包含分隔符
    res = bytes.Fields(str2)
    fmt.Println(res)
    for _,v := range res{
    fmt.Println(string(v))
    }

    // 以符合 f 的字符为分隔符将 s 切分成多个子串,结果不包含分隔符
    res = bytes.FieldsFunc(str2,func(r rune)bool{
    if r=='l'{
    return true
    }else{
    return false
    }
    })
    for _,v := range res{
    fmt.Println(string(v))
    }

    // 用连接符,将子串列表连接成一个字节串
    fmt.Println(string(bytes.Join(res,[]byte("|"))))
    //重复"a"10次
    fmt.Println(string(bytes.Repeat([]byte("a"),10)))



    str3 := []byte("hello world")
    //前后缀判断是否包含
    fmt.Println(bytes.HasPrefix(str3,[]byte("h")))
    fmt.Println(bytes.HasSuffix(str3,[]byte("h")))

    // 判断 b 中是否包含子串 subslice(字符 r)
    fmt.Println(bytes.Contains(str3,[]byte("h")))

    //查找第一次出现位置
    fmt.Println(bytes.Index(str3,[]byte("l")))
    fmt.Println(bytes.IndexByte(str3,108))
    fmt.Println(bytes.IndexRune(str3,'l'))
    //找不到返回-1
    fmt.Println(bytes.IndexAny(str3,"l"))
    fmt.Println(bytes.IndexAny(str3,"a"))

    resInt := bytes.IndexFunc(str3, func(r rune) bool {
    if r == 'l' {
    return true
    } else {
    return false
    }
    })
    fmt.Println(resInt)

    // 功能同上,只不过查找最后一次出现的位置。
    //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

    // 查找l在字符中出现的次数
    fmt.Println(bytes.Count(str3,[]byte("l")))

    //替换字符
    //func Replace(s, old, new []byte, n int) []byte
    fmt.Println(string(bytes.Replace(str3,[]byte("l"),[]byte("--replace--"),5)))

    r1 := bytes.Map(func(r rune) rune {
    if r == 'l'{
    return '|' //替换
    } else if r == 'e' {
    return -1 //负值 将丢弃字符
    }

    return r //正常返回不操作

    },str3)
    fmt.Println(string(r1))


    fmt.Println(bytes.Runes(str3))

    }
    func mapReplace(r rune) rune {
    if r == 'l'{
    return 'a'
    } else {
    return 'b'
    }
    }