概況:
包字節實現了操作字節切片的函數。它類似於琴弦包的設施。
函數:
- 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\n") } a = []byte{2} b = []byte{2} if bytes.Compare(a,b) <= 0 { fmt.Println("a <= b\n") } a = []byte{3} b = []byte{2} if bytes.Compare(a,b) >= 0 { fmt.Println("a >= b \n") } if bytes.Equal(a,b) { fmt.Println(" a == b \n") } if !bytes.Equal(a,b) { fmt.Println(" a not equal b \n") } }
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(""))) }