概况:
包字节实现了操作字节切片的函数。它类似于琴弦包的设施。
函数:
- func Compare(a, b []byte) int
- func Contains(b, subslice []byte) bool
- func ContainsAny(b []byte, chars string) bool
- func ContainsRune(b []byte, r rune) bool
- func Count(s, sep []byte) int
- func Equal(a, b []byte) bool
- func EqualFold(s, t []byte) bool
- func Fields(s []byte) [][]byte
- func FieldsFunc(s []byte, f func(rune) bool) [][]byte
- func HasPrefix(s, prefix []byte) bool
- func HasSuffix(s, suffix []byte) bool
- func Index(s, sep []byte) int
- func IndexAny(s []byte, chars string) int
- func IndexByte(b []byte, c byte) int
- func IndexFunc(s []byte, f func(r rune) bool) int
- func IndexRune(s []byte, r rune) int
- func Join(s [][]byte, sep []byte) []byte
- func LastIndex(s, sep []byte) int
- func LastIndexAny(s []byte, chars string) int
- func LastIndexByte(s []byte, c byte) int
- func LastIndexFunc(s []byte, f func(r rune) bool) int
- func Map(mapping func(r rune) rune, s []byte) []byte
- func Repeat(b []byte, count int) []byte
- func Replace(s, old, new []byte, n int) []byte
- func ReplaceAll(s, old, new []byte) []byte
- func Runes(s []byte) []rune
- 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
package main import ( "bytes" "fmt" ) //bytes.Compare 比较返回一个按字典顺序比较两个字节切片的整数。如果a == b则结果为0,如果a <b则结果为-1,如果a> b则结果为+1。 nil参数等同于空切片 func main() { var a,b []byte a = []byte{1} b = []byte{2} if bytes.Compare(a,b) < 0 { fmt.Println(" a < b ") } a = []byte{2} b = []byte{2} if bytes.Compare(a,b) <= 0 { fmt.Println("a <= b ") } a = []byte{3} b = []byte{2} if bytes.Compare(a,b) >= 0 { fmt.Println("a >= b ") } if bytes.Equal(a,b) { fmt.Println(" a == b ") } if !bytes.Equal(a,b) { fmt.Println(" a not equal b ") } }
package main import ( "bytes" "fmt" ) //func Contains(b, subslice []byte) bool //bytes.Constains 包含报告子切片是否在b内。 func main() { var s1,s2 []byte s1 = []byte("abcfoo") s2 = []byte("abc") if bytes.Contains(s1,s2) { fmt.Println(" s1 constains s2 ") } fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo"))) fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar"))) fmt.Println(bytes.Contains([]byte("seafood"), []byte(""))) fmt.Println(bytes.Contains([]byte(""), []byte(""))) }