Expand description
Package bytes implements functions for the manipulation of byte slices. It is analogous to the facilities of the strings package.
zh-cn
bytes包实现了操作[]byte的常用函数。本包的函数和strings包的函数相当类似。Structs§
- Buffer
- A Buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use.
- Reader
- A Reader implements the io.Reader, io.ReaderAt, io.ByteReader, io.ByteScanner, io.RuneReader, io.RuneScanner, io.Seeker, and io.WriterTo interfaces by reading from a string. The zero value for Reader operates like a Reader of an empty string.
- Replacer
- Replacer replaces a list of slices with replacements. It is safe for concurrent use by multiple goroutines.
Functions§
- Compare
- Compare returns an integer comparing two byte slices lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
- Contains
- Contains reports whether subslice is within b.
- Contains
Any - ContainsAny reports whether any of the UTF-8-encoded code points in chars are within b.
- Contains
Rune - ContainsRune reports whether the rune is contained in the UTF-8-encoded byte slice b.
- Count
- Count counts the number of non-overlapping instances of sep in s. If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.
- Cut
- Cut slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, “”, false.
- Fields
- Fields interprets s as a sequence of UTF-8-encoded code points. It splits the slice s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of subslices of s or an empty slice if s contains only white space.
- Fields
Func - FieldsFunc interprets s as a sequence of UTF-8-encoded code points. It splits the slice s at each run of code points c satisfying f(c) and returns a slice of subslices of s. If all code points in s satisfy f(c), or len(s) == 0, an empty slice is returned.
- HasPrefix
- HasPrefix tests whether the byte slice s begins with prefix.
- HasSuffix
- HasSuffix tests whether the byte slice s ends with suffix.
- Index
- Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
- Index
Any - IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the first occurrence in s of any of the Unicode code points in chars. It returns -1 if chars is empty or if there is no code point in common.
- Index
Byte - IndexByte returns the index of the first instance of c in b, or -1 if c is not present in b.
- Index
Func - IndexFunc interprets s as a sequence of UTF-8-encoded code points. It returns the byte index in s of the first Unicode code point satisfying f(c), or -1 if none do.
- Index
Rune - IndexRune interprets s as a sequence of UTF-8-encoded code points. It returns the byte index of the first occurrence in s of the given rune. It returns -1 if rune is not present in s. If r is utf8.RuneError, it returns the first instance of any invalid UTF-8 byte sequence.
- Join
- Join concatenates the elements of s to create a new byte slice. The separator sep is placed between elements in the resulting slice.
- Last
Index - LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
- Last
Index Any - LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the last occurrence in s of any of the Unicode code points in chars. It returns -1 if chars is empty or if there is no code point in common.
- Last
Index Byte - LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
- Last
Index Func - LastIndexFunc interprets s as a sequence of UTF-8-encoded code points. It returns the byte index in s of the last Unicode code point satisfying f(c), or -1 if none do.
- Map
- Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.
- Repeat
- Repeat returns a new string consisting of count copies of the string s.
- Replace
- Replace returns a copy of the slice s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the slice and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune slice. If n = -1, there is no limit on the number of replacements.
- Replace
All - ReplaceAll returns a copy of the slice s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the slice and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune slice.
- Split
- Split slices s into all subslices separated by sep and returns a slice of the subslices between those separators. If sep is empty, Split splits after each UTF-8 sequence. It is equivalent to SplitN with a count of -1.
- Split
After - SplitAfter slices s into all subslices after each instance of sep and returns a slice of those subslices. If sep is empty, SplitAfter splits after each UTF-8 sequence. It is equivalent to SplitAfterN with a count of -1.
- Split
AfterN - SplitAfterN slices s into subslices after each instance of sep and returns a slice of those subslices. If sep is empty, SplitAfterN splits after each UTF-8 sequence. The count determines the number of subslices to return:
- SplitN
- SplitN slices s into subslices separated by sep and returns a slice of the subslices between those separators. If sep is empty, SplitN splits after each UTF-8 sequence.
- ToLower
- ToLower returns s with all Unicode letters mapped to their lower case.
- ToUpper
- ToUpper returns s with all Unicode letters mapped to their upper case.
- Trim
- Trim returns a subslice of s by slicing off all leading and trailing UTF-8-encoded code points contained in cutset.
- Trim
Func - TrimFunc returns a subslice of s by slicing off all leading and trailing UTF-8-encoded code points c that satisfy f(c).
- Trim
Left - TrimLeft returns a subslice of s by slicing off all leading UTF-8-encoded code points contained in cutset.
- Trim
Left Func - TrimLeftFunc treats s as UTF-8-encoded bytes and returns a subslice of s by slicing off all leading UTF-8-encoded code points c that satisfy f(c).
- Trim
Prefix - TrimPrefix returns s without the provided leading prefix slices. If s doesn’t start with prefix, s is returned unchanged.
- Trim
Right - TrimRight returns a subslice of s by slicing off all trailing UTF-8-encoded code points that are contained in cutset.
- Trim
Right Func - TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8-encoded code points c that satisfy f(c).
- Trim
Space - TrimSpace returns a subslice of s by slicing off all leading and trailing white space, as defined by Unicode.
- Trim
Suffix - TrimSuffix returns s without the provided trailing suffix string. If s doesn’t end with suffix, s is returned unchanged.