strings
21 分钟阅读
Package strings implements simple functions to manipulate UTF-8 encoded strings.
strings
包实现了一些简单的函数来操作 UTF-8 编码的字符串。
For information about UTF-8 strings in Go, see https://blog.golang.org/strings.
有关 Go 中的 UTF-8 字符串的信息,请参阅 https://blog.golang.org/strings。
常量
This section is empty.
变量
This section is empty.
函数
func Clone <- go1.18
|
|
Clone returns a fresh copy of s. It guarantees to make a copy of s into a new allocation, which can be important when retaining only a small substring of a much larger string. Using Clone can help such programs use less memory. Of course, since using Clone makes a copy, overuse of Clone can make programs use more memory. Clone should typically be used only rarely, and only when profiling indicates that it is needed. For strings of length zero the string "" will be returned and no allocation is made.
Clone函数返回 s 的一个全新副本。它保证将 s 复制到一个新的分配中,当仅保留一个更大字符串的小子串时,这可能很重要。使用 Clone函数可以帮助这些程序使用更少的内存。当然,由于使用 Clone函数会进行复制,过度使用 Clone函数可能会使程序使用更多的内存。通常应仅在分析表明需要时才使用 Clone函数。对于长度为零的字符串,将返回字符串 “",并且不会进行任何分配。
func Compare <- go1.5
|
|
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函数按字典顺序比较两个字符串。
如果 a == b,则结果为 0;
如果 a < b,则结果为 -1;
如果 a > b,则结果为 +1。
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.
Compare 函数只是为了与 bytes 包保持对称性而被包含进来的。使用内置的字符串比较运算符 ==、<、> 等通常更加清晰明了,而且速度更快。
Compare Example
|
|
func Contains
|
|
Contains reports whether substr is within s.
Contains函数报告 substr 是否在 s 中。
Contains Example
|
|
func ContainsAny
|
|
ContainsAny reports whether any Unicode code points in chars are within s.
ContainsAny函数报告 s 中是否包含 chars 中的任何 Unicode 码点。
ContainsAny Example
|
|
func ContainsFunc <-go1.21.0
|
|
ContainsFunc reports whether any Unicode code points r within s satisfy f(r).
ContainsFunc 判断字符串 s 中是否有任意 Unicode 码点 r 满足函数 f(r) 的条件。
func ContainsRune
|
|
ContainsRune reports whether the Unicode code point r is within s.
ContainsRune函数报告 Unicode 码点 r 是否在 s 中。
ContainsRune Example
|
|
func 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.
Count函数返回 s 中不重叠的 substr 实例数。如果 substr 是空字符串,则 Count 返回 s 中 Unicode 码点的数量加 1。
Count Example
|
|
func Cut <- go1.18
|
|
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.
Cut函数围绕第一个 sep 实例对 s 进行切片,返回 sep 之前和之后的文本。found 结果报告 sep 是否出现在 s 中。如果 sep 不出现在 s 中,则 Cut 返回 s、""、false。
Cut Example
|
|
func CutPrefix <- go1.20
|
|
CutPrefix returns s without the provided leading prefix string and reports whether it found the prefix. If s doesn’t start with prefix, CutPrefix returns s, false. If prefix is the empty string, CutPrefix returns s, true.
CutPrefix函数返回去掉前缀字符串 prefix 后的 s,并报告它是否找到了前缀。如果 s 不以 prefix 开头,则 CutPrefix函数返回 s、false。如果 prefix 是空字符串,则 CutPrefix 返回 s、true。
func CutSuffix <- go1.20
|
|
CutSuffix returns s without the provided ending suffix string and reports whether it found the suffix. If s doesn’t end with suffix, CutSuffix returns s, false. If suffix is the empty string, CutSuffix returns s, true.
CutSuffix函数返回s中不包含指定结尾后缀字符串的部分,并报告它是否找到了后缀。如果s不以后缀结尾,则CutSuffix函数返回s,false。如果后缀为空字符串,则CutSuffix函数返回s,true。
func EqualFold
|
|
EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.
EqualFold函数报告s和t在简单的Unicode折叠(一种更一般的不区分大小写的形式)下是否相等,这是一种更一般的不区分大小写的形式。
EqualFold Example
|
|
func 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函数将字符串s按一个或多个连续的空格字符(由unicode.IsSpace定义)分割,返回s的子字符串切片或一个空切片,如果s仅包含空格,则返回空切片。
Fields Example
|
|
func FieldsFunc
|
|
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.
FieldsFunc函数将字符串s在每个运行满足f(c) 的Unicode码点c处分割,并返回s的切片数组。如果s中所有码点都满足f(c) 或字符串为空,则返回空切片。
FieldsFunc makes no guarantees about the order in which it calls f(c) and assumes that f always returns the same value for a given c.
FieldsFunc函数不保证调用f(c) 的顺序,并假定f始终为给定c返回相同的值。
FieldsFunc Example
|
|
func HasPrefix
|
|
HasPrefix tests whether the string s begins with prefix.
HasPrefix函数测试字符串s是否以prefix开头。
HasPrefix Example
|
|
func HasSuffix
|
|
HasSuffix tests whether the string s ends with suffix.
HasSuffix函数测试字符串s是否以后缀suffix结尾。
HasSuffix Example
|
|
func Index
|
|
Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
Index函数返回substr在s中第一次出现的索引,如果substr不在s中,则返回-1。
Index Example
|
|
func IndexAny
|
|
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.
IndexAny函数返回s中任何Unicode码点中chars的第一个实例的索引,如果s中不存在来自chars的Unicode码点,则返回-1。
IndexAny Example
|
|
func IndexByte <- go1.2
|
|
IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
IndexByte函数返回c在s中第一次出现的索引,如果c不在s中,则返回-1。
IndexByte Example
|
|
func IndexFunc
|
|
IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.
IndexFunc函数返回第一个满足f(c) 的Unicode码点c在s中的索引,如果没有则返回-1。
IndexFunc Example
|
|
func IndexRune
|
|
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.
IndexRune函数返回Unicode码点r在字符串s中第一次出现的索引,如果r不在s中,则返回-1。如果r是utf8.RuneError,则返回任何无效的UTF-8字节序列的第一个实例。
IndexRune Example
|
|
func 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.
Join函数将其第一个参数的元素连接起来以创建单个字符串。分隔符字符串sep放置在结果字符串中的元素之间。
Join Example
|
|
func LastIndex
|
|
LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
LastIndexByte函数返回c在s中最后一次出现的索引,如果c不在s中,则返回-1。
LastIndex Example
|
|
func LastIndexAny
|
|
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.
LastIndexAny函数返回 chars 中任何 Unicode 码点在 s 中最后一次出现的索引,如果 s 中没有来自 chars 的 Unicode 码点,则返回 -1。
LastIndexAny Example
|
|
func LastIndexByte <- go1.5
|
|
LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
LastIndexByte函数返回 c 在 s 中最后一次出现的索引,如果 c 不在 s 中,则返回 -1。
LastIndexByte Example
|
|
func LastIndexFunc
|
|
LastIndexFunc returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.
LastIndexFunc函数返回最后一个满足 f(c) 的 Unicode 码点在 s 中的索引,如果没有,则返回-1。
LastIndexFunc Example
|
|
func 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.
Map函数返回一个新的字符串,其中包含根据映射函数修改的 s 中的所有字符。如果 mapping 返回负值,则删除该字符而不替换。(例如返回-1
)
Map Example
|
|
func Repeat
|
|
Repeat returns a new string consisting of count copies of the string s.
Repeat函数返回由 count 个字符串 s 组成的新字符串。
It panics if count is negative or if the result of (len(s) * count) overflows.
如果 count 为负数或 (len(s) * count)
的结果溢出,则会发生 panic。
Repeat Example
|
|
func 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.
Replace函数将字符串s中前n
个非重叠old实例替换为new,并返回新的字符串。如果old为空,则它匹配字符串开头和每个UTF-8序列之后,为k个rune字符串提供最多k+1
个替换。如果n < 0,则不限制替换次数。
Replace Example
|
|
func ReplaceAll <- go1.12
|
|
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.
ReplaceAll函数将字符串s中所有非重叠old实例替换为new,并返回新的字符串。如果old为空,则它匹配字符串开头和每个UTF-8序列之后,为k个rune字符串提供最多k+1个替换。
ReplaceAll Example
|
|
func Split
|
|
Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
Split函数将字符串s按sep分割成所有子字符串,并返回它们之间的子字符串切片。
If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.
如果s不包含sep且sep不为空,则Split返回长度为1的切片,其中唯一的元素为s。
If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.
如果sep为空,则Split在每个UTF-8序列之后拆分。如果s和sep都为空,则Split返回一个空切片。
It is equivalent to SplitN with a count of -1.
它等价于带有计数-1的SplitN函数。
To split around the first instance of a separator, see Cut.
要在第一个分隔符实例周围分割,请参见Cut 函数。
Split Example
|
|
func SplitAfter
|
|
SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings.
SplitAfter函数将字符串s按sep分割成每个实例后的所有子字符串,并返回它们的子字符串切片。
If s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s.
如果s不包含sep且sep不为空,则SplitAfter返回长度为1的切片,其中唯一的元素为s。
If sep is empty, SplitAfter splits after each UTF-8 sequence. If both s and sep are empty, SplitAfter returns an empty slice.
如果sep为空,则SplitAfter在每个UTF-8序列之后拆分。如果s和sep都为空,则SplitAfter返回一个空切片。
It is equivalent to SplitAfterN with a count of -1.
它等价于带有计数-1
的SplitAfterN函数。
SplitAfter Example
|
|
func SplitAfterN
|
|
SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings.
SplitAfterN函数将字符串s按sep拆分为每个实例之后的子字符串,并返回它们的子字符串切片。
The count determines the number of substrings to return:
计数参数n确定要返回的子字符串的数量:
n > 0:最多n个子字符串;最后一个子字符串将是未拆分的剩余部分。
n == 0:结果为nil(零个子字符串)
n < 0:所有子字符串
Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for SplitAfter.
s和sep的边缘情况(例如,空字符串)的处理方式如SplitAfter函数文档中所述。
SplitAfterN Example
|
|
func SplitN
|
|
SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.
SplitN函数将字符串s按sep分割为每个分隔符之间的子字符串,并返回它们的子字符串切片。
The count determines the number of substrings to return:
计数参数n确定要返回的子字符串的数量:
n > 0:最多n个子字符串;最后一个子字符串将是未拆分的剩余部分。
n == 0:结果为nil(零个子字符串)
n < 0:所有子字符串
Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for Split.
对于 s 和 sep 的边界情况(例如空字符串),将按照 Split函数文档中描述的方式处理。
To split around the first instance of a separator, see Cut.
如果要在第一个分隔符周围进行分割,请参阅 Cut函数。
SplitN Example
|
|
func Title <- DEPRECATED
|
|
Title returns a copy of the string s with all Unicode letters that begin words mapped to their Unicode title case.
Deprecated: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.
Title Example
|
|
func ToLower
|
|
ToLower returns s with all Unicode letters mapped to their lower case.
ToLower函数返回s的所有Unicode字母均被映射为它们的小写形式的副本。
ToLower Example
|
|
func ToLowerSpecial
|
|
ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their lower case using the case mapping specified by c.
ToLowerSpecial函数返回s的所有Unicode字母均被映射为它们的小写形式的副本,使用由c指定的大小写映射。
ToLowerSpecial Example
|
|
func ToTitle
|
|
ToTitle returns a copy of the string s with all Unicode letters mapped to their Unicode title case.
ToTitle函数返回s的所有Unicode字母均被映射为它们的Unicode标题形式的副本。
ToTitle Example
|
|
func ToTitleSpecial
|
|
ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their Unicode title case, giving priority to the special casing rules.
ToTitleSpecial函数返回s的所有Unicode字母均被映射为它们的Unicode标题形式的副本,优先考虑特殊的大小写规则。
ToTitleSpecial Example
|
|
func ToUpper
|
|
ToUpper returns s with all Unicode letters mapped to their upper case.
ToUpper函数返回s的所有Unicode字母均被映射为它们的大写形式的副本。
ToUpper Example
|
|
func ToUpperSpecial
|
|
ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their upper case using the case mapping specified by c.
ToUpperSpecial函数返回s的所有Unicode字母均被映射为它们的大写形式的副本,使用由c指定的大小写映射。
ToUpperSpecial Example
|
|
func ToValidUTF8 <- go1.13
|
|
ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement string, which may be empty.
ToValidUTF8函数返回s的每个无效UTF-8字节序列的运行都被替换为替换字符串的副本,替换字符串可以为空。
func Trim
|
|
Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
Trim函数返回字符串s的所有前导和尾随Unicode码点都包含在cutset中,这些码点被删除的切片。
Trim Example
|
|
func TrimFunc
|
|
TrimFunc returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.
TrimFunc函数返回字符串 s 中所有符合函数 f 的 Unicode 码点的前缀和后缀都被删除后的子字符串。
TrimFunc Example
|
|
func TrimLeft
|
|
TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
TrimLeft函数返回字符串 s 去掉所有前导的包含在 cutset 中的 Unicode 码点后的子字符串。
To remove a prefix, use TrimPrefix instead.
如果要去掉一个前缀,请使用 TrimPrefix函数。
TrimLeft Example
|
|
func TrimLeftFunc
|
|
TrimLeftFunc returns a slice of the string s with all leading Unicode code points c satisfying f(c) removed.
TrimLeftFunc函数返回字符串 s 去掉所有前导符合函数 f 的 Unicode 码点后的子字符串。
TrimLeftFunc Example
|
|
func TrimPrefix <- go1.1
|
|
TrimPrefix returns s without the provided leading prefix string. If s doesn’t start with prefix, s is returned unchanged.
TrimPrefix函数返回字符串 s 去掉提供的前缀字符串 prefix。如果 s 不以 prefix 开头,则返回 s 不变。
TrimPrefix Example
|
|
func TrimRight
|
|
TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.
TrimRight函数返回字符串 s 去掉所有后缀的包含在 cutset 中的 Unicode 码点后的子字符串。
To remove a suffix, use TrimSuffix instead.
如果要去掉一个后缀,请使用 TrimSuffix函数。
TrimRight Example
|
|
func TrimRightFunc
|
|
TrimRightFunc returns a slice of the string s with all trailing Unicode code points c satisfying f(c) removed.
TrimRightFunc函数返回字符串 s 去掉所有后缀符合函数 f 的 Unicode 码点后的子字符串。
TrimRightFunc Example
|
|
func TrimSpace
|
|
TrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.
TrimSpace函数返回字符串 s 去掉所有前导和后缀的空白字符,根据 Unicode 定义。
TrimSpace Example
|
|
func TrimSuffix <- go1.1
|
|
TrimSuffix returns s without the provided trailing suffix string. If s doesn’t end with suffix, s is returned unchanged.
TrimSuffix函数返回字符串 s 去掉提供的后缀字符串 suffix。如果 s 不以 suffix 结尾,则返回 s 不变。
TrimSuffix Example
|
|
类型
type Builder <- go1.10
|
|
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.
Builder结构体用于使用 Write 方法高效地构建字符串。它最小化了内存复制。零值可直接使用。不要复制非零值 Builder。
Example
|
|
(*Builder) Cap <- go1.12
|
|
Cap returns the capacity of the builder’s underlying byte slice. It is the total space allocated for the string being built and includes any bytes already written.
Cap方法返回 builder 底层字节切片的容量。它是为正在构建的字符串分配的总空间,包括已经写入的任何字节。
(*Builder) Grow <- go1.10
|
|
Grow grows b’s capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to b without another allocation. If n is negative, Grow panics.
Grow方法按需增加 b 的容量,以保证另外 n 个字节的空间。调用 Grow(n) 后,至少可以将 n 个字节写入 b,而不必另行分配。如果 n 为负数,则 Grow 会出现 panic。
(*Builder) Len <- go1.10
|
|
Len returns the number of accumulated bytes; b.Len() == len(b.String()).
Len方法返回已经累积的字节数;b.Len() == len(b.String())。
(*Builder) Reset <- go1.10
|
|
Reset resets the Builder to be empty.
Reset方法将 Builder 重置为空。
(*Builder) String <- go1.10
|
|
String returns the accumulated string.
String方法返回已经累积的字符串。
(*Builder) Write <- go1.10
|
|
Write appends the contents of p to b’s buffer. Write always returns len(p), nil.
Write方法将 p 的内容追加到 b 的缓冲区中。Write 总是返回 len(p) 和 nil。
(*Builder) WriteByte <- go1.10
|
|
WriteByte appends the byte c to b’s buffer. The returned error is always nil.
WriteByte方法将字节 c 追加到 b 的缓冲区中。返回的错误始终为 nil。
(*Builder) WriteRune <- go1.10
|
|
WriteRune appends the UTF-8 encoding of Unicode code point r to b’s buffer. It returns the length of r and a nil error.
WriteRune方法将 Unicode 码点 r 的 UTF-8 编码附加到 b 的缓冲区中。它返回 r 的长度和 nil 错误。
(*Builder) WriteString <- go1.10
|
|
WriteString appends the contents of s to b’s buffer. It returns the length of s and a nil error.
WriteString方法将字符串s的内容附加到b的缓冲区中。它返回s的长度和一个nil错误。
type 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.
Reader结构体通过从字符串中读取数据来实现io.Reader、io.ReaderAt、io.ByteReader、io.ByteScanner、io.RuneReader、io.RuneScanner、io.Seeker和io.WriterTo接口。Reader的零值像一个空字符串的Reader。
func NewReader
|
|
NewReader returns a new Reader reading from s. It is similar to bytes.NewBufferString but more efficient and read-only.
NewReader函数返回一个从s读取的新Reader。它类似于bytes.NewBufferString,但更高效且只读。
(*Reader) Len
|
|
Len returns the number of bytes of the unread portion of the string.
Len方法返回未读部分字符串的字节数。
(*Reader) Read
|
|
Read implements the io.Reader interface.
Read方法实现了io.Reader接口。
(*Reader) ReadAt
|
|
ReadAt implements the io.ReaderAt interface.
ReadAt方法实现了io.ReaderAt接口。
(*Reader) ReadByte
|
|
ReadByte implements the io.ByteReader interface.
ReadByte方法实现了io.ByteReader接口。
(*Reader) ReadRune
|
|
ReadRune implements the io.RuneReader interface.
ReadRune方法实现了io.RuneReader接口。
(*Reader) Reset <- go1.7
|
|
Reset resets the Reader to be reading from s.
Reset方法将Reader重置为从s中读取。
(*Reader) Seek
|
|
Seek implements the io.Seeker interface.
Seek方法实现了io.Seeker接口。
(*Reader) Size <- go1.5
|
|
Size returns the original length of the underlying string. Size is the number of bytes available for reading via ReadAt. The returned value is always the same and is not affected by calls to any other method.
Size方法返回底层字符串的原始长度。Size方法是通过ReadAt方法可读取的字节数。返回值总是相同的,并不受任何其他方法的调用影响。
(*Reader) UnreadByte
|
|
UnreadByte implements the io.ByteScanner interface.
UnreadByte方法实现了 io.ByteScanner 接口。
(*Reader) UnreadRune
|
|
UnreadRune implements the io.RuneScanner interface.
UnreadRune方法实现了 io.RuneScanner 接口。
(*Reader) WriteTo <- go1.1
|
|
WriteTo implements the io.WriterTo interface.
WriteTo方法实现了 io.WriterTo 接口。
type Replacer
|
|
Replacer replaces a list of strings with replacements. It is safe for concurrent use by multiple goroutines.
Replacer结构体可以用一组字符串替换另一组字符串。它可以被多个 goroutine 并发使用。
func NewReplacer
|
|
NewReplacer returns a new Replacer from a list of old, new string pairs. Replacements are performed in the order they appear in the target string, without overlapping matches. The old string comparisons are done in argument order.
NewReplacer函数通过一组 old,new 字符串对返回一个新的 Replacer。替换按目标字符串中它们出现的顺序执行,而不会重叠匹配。old 字符串的比较按参数顺序执行。
NewReplacer panics if given an odd number of arguments.
如果传入奇数个参数,NewReplacer函数将 panic。
NewReplacer Example
|
|
(*Replacer) Replace
|
|
Replace returns a copy of s with all replacements performed.
Replace方法返回执行所有替换后的 s 的副本。
(*Replacer) WriteString
|
|
WriteString writes s to w with all replacements performed.
WriteString方法将 s 写入 w,执行所有替换。