Expand description
Package strings implements simple functions to manipulate UTF-8 encoded strings.
zh-cn
strings 实现了操作UTF-8编码字符串的简单函数。Structs§
- Builder
- A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero Builder.
- 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 strings with replacements. It is safe for concurrent use by multiple goroutines.
Functions§
- Compare
- Compare returns an integer comparing two strings lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b. Compare is included only for symmetry with package bytes. It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.
- Contains
- Contains reports whether substr is within s.
- Contains
Any - ContainsAny reports whether any Unicode code points in chars are within s.
- Contains
Rune - ContainsRune reports whether the Unicode code point r is within s.
- Count
- Count counts the number of non-overlapping instances of substr in s. If substr is an empty string, Count returns 1 + the number of Unicode 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.
- Equal
Fold - EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity.
- Fields
- Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.
- Fields
Func - FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned.
- HasPrefix
- HasPrefix tests whether the string s begins with prefix.
- HasSuffix
- HasSuffix tests whether the string s ends with suffix.
- Index
- Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
- Index
Any - IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
- Index
Byte - IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
- Index
Func - IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.
- Index
Rune - IndexRune returns the index of the first instance of the Unicode code point r, or -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 its first argument to create a single string. The separator string sep is placed between elements in the resulting string.
- Last
Index - LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
- Last
Index Any - LastIndexAny returns the index of the last instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
- 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 returns the index into 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 string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements. It panics if count is negative or if the result of (len!(s) * count) overflows.
- Replace
All - ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.
- Split
- Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
- Split
After - SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings.
- Split
AfterN - SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings.
- SplitN
- SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.
- ToLower
- ToLower returns s with all Unicode letters mapped to their lower case.
- ToTitle
- ToTitle returns a copy of the string s with all Unicode letters mapped to their Unicode title case.
- ToUpper
- ToUpper returns s with all Unicode letters mapped to their upper case.
- Trim
- Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
- Trim
Func - TrimFunc returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.
- Trim
Left - TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
- Trim
Left Func - TrimLeftFunc returns a slice of the string s with all leading Unicode code points c satisfying f(c) removed.
- Trim
Prefix - TrimPrefix returns s without the provided leading prefix string. If s doesn’t start with prefix, s is returned unchanged.
- Trim
Right - TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.
- Trim
Right Func - TrimRightFunc returns a slice of the string s with all trailing Unicode code points c satisfying f(c) removed.
- Trim
Space - TrimSpace returns a slice of the string s, with all leading and trailing white space removed, 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.