go bytes包

代码:

package main

import (
"bytes"
"fmt"
// "icode.baidu.com/baidu/gdp/automaxprocs"
//
"icode.baidu.com/baidu/gdp/log"
)

// "icode.baidu.com/baidu/gdp/log"

// "icode.baidu.com/baidu/gdp/log"

func main() {
println("hello,world")
buff1 := make([]byte, 2)
writeInt16(buff1, uint16(15))

buff2 := make([]byte, 2)
writeInt16(buff2, uint16(13))
res := bytes.Compare(buff1, buff2)
fmt.Println(res)

buff3 := []byte("hello world hello world")
seq := []byte("hello")
res = bytes.Count(buff3, seq)
fmt.Println(res)

contains := bytes.Contains(buff3, seq)
println(contains)

res = bytes.Index(buff3, seq)
println(res)

res = bytes.LastIndex(buff3, seq)
println(res)

a := rune('e')
res = bytes.IndexRune(buff3, a)
println("indexRune:", res)

println("------------")
buff5 := []byte("abcabcabcabc")

arr := bytes.SplitN(buff5, []byte("a"), 3)
for _, v := range arr {
    for _, t := range v {
        print(t)
        print(",")
    }
    println("|")
}

println("------------")
buff6 := []byte("abc abc abc        abc")
arr = bytes.Fields(buff6)
for _, v := range arr {
    for _, t := range v {
        print(t)
        print(",")
    }
    println("|")
}

println("------------")
buff7 := []byte("abcabcabcabc")
arr = bytes.FieldsFunc(buff7, spilt)
for _, v := range arr {
    for _, t := range v {
        print(t)
        print(",")
    }
    println("|")
}

buff8 := []byte("我是中国人")
data := bytes.Runes(buff8)
for _, elem := range data {
    println(string(elem))
}

buff9 := bytes.Title(buff7)
println(string(buff9))

buff10 := bytes.Map(func(r rune) rune {
    if r == 'c' {
        return 'a'
    }
    return r
}, buff7)
println(string(buff10))

}

func spilt(r rune) bool {
if r == 'c' {
return true
}
return false
}

func writeInt16(buff []byte, data uint16) {
for i := 0; i < 2; i++ {
buff[i] = byte(data >> uint(i*8))
}
}

func printBytes(varBytes []byte) {
strByte := string(varBytes)
fmt.Println(strByte)
}

输出

hello,world
1
2
true
0
12
indexRune: 1

|
98,99,|
98,99,97,98,99,97,98,99,|

97,98,99,|
97,98,99,|
97,98,99,|
97,98,99,|

97,98,|
97,98,|
97,98,|
97,98,|





Abcabcabcabc
abaabaabaaba

上一篇:98. 验证二叉搜索树


下一篇:97-98