golang bytes 包 详解

概况:

包字节实现了操作字节切片的函数。它类似于琴弦包的设施。

函数:

  1. func Compare(a, b []byte) int
  2. func Contains(b, subslice []byte) bool
  3. func ContainsAny(b []byte, chars string) bool
  4. func ContainsRune(b []byte, r rune) bool
  5. func Count(s, sep []byte) int
  6. func Equal(a, b []byte) bool
  7. func EqualFold(s, t []byte) bool
  8. func Fields(s []byte) [][]byte
  9. func FieldsFunc(s []byte, f func(rune) bool) [][]byte
  10. func HasPrefix(s, prefix []byte) bool
  11. func HasSuffix(s, suffix []byte) bool
  12. func Index(s, sep []byte) int
  13. func IndexAny(s []byte, chars string) int
  14. func IndexByte(b []byte, c byte) int
  15. func IndexFunc(s []byte, f func(r rune) bool) int
  16. func IndexRune(s []byte, r rune) int
  17. func Join(s [][]byte, sep []byte) []byte
  18. func LastIndex(s, sep []byte) int
  19. func LastIndexAny(s []byte, chars string) int
  20. func LastIndexByte(s []byte, c byte) int
  21. func LastIndexFunc(s []byte, f func(r rune) bool) int
  22. func Map(mapping func(r rune) rune, s []byte) []byte
  23. func Repeat(b []byte, count int) []byte
  24. func Replace(s, old, new []byte, n int) []byte
  25. func ReplaceAll(s, old, new []byte) []byte
  26. func Runes(s []byte) []rune
  27. func Split(s, sep []byte) [][]byte
  28. func SplitAfter(s, sep []byte) [][]byte
  29. func SplitAfterN(s, sep []byte, n int) [][]byte
  30. func SplitN(s, sep []byte, n int) [][]byte
  31. func Title(s []byte) []byte
  32. func ToLower(s []byte) []byte
  33. func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte
  34. func ToTitle(s []byte) []byte
  35. func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte
  36. func ToUpper(s []byte) []byte
  37. func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte
  38. func Trim(s []byte, cutset string) []byte
  39. func TrimFunc(s []byte, f func(r rune) bool) []byte
  40. func TrimLeft(s []byte, cutset string) []byte
  41. func TrimLeftFunc(s []byte, f func(r rune) bool) []byte
  42. func TrimPrefix(s, prefix []byte) []byte
  43. func TrimRight(s []byte, cutset string) []byte
  44. func TrimRightFunc(s []byte, f func(r rune) bool) []byte
  45. func TrimSpace(s []byte) []byte
  46. 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("")))
}

  

猜你喜欢

转载自www.cnblogs.com/liujie-php/p/10674764.html
今日推荐