strings
1 函数
1.1 字符串基础操作
1.1.1 func Clone(s string) string
s1 := "Hello, 世界"
s2 := strings.Clone(s1)
fmt.Println(s2) // Hello, 世界1.1.2 func Compare(a, b string) int
fmt.Println(strings.Compare("apple", "banana")) // -1
fmt.Println(strings.Compare("", "")) // 01.2 包含性检查
1.2.1 func Contains(s, substr string) bool
1.2.2 func ContainsAny(s, chars string) bool
1.2.3 func ContainsFunc(s string, f func(rune) bool) bool
1.2.4 func ContainsRune(s string, r rune) bool
1.3 计数与分割
1.3.1 func Count(s, substr string) int
1.3.2 func Cut(s, sep string) (before, after string, found bool)
1.3.3 func CutPrefix(s, prefix string) (after string, found bool)
1.3.4 func CutSuffix(s, suffix string) (before string, found bool)
1.4 大小写处理
1.4.1 func EqualFold(s, t string) bool
1.4.2 func ToLower(s string) string
1.4.3 func ToUpper(s string) string
1.5 分割与连接
1.5.1 func Fields(s string) []string
1.5.2 func FieldsFunc(s string, f func(rune) bool) []string
1.5.3 func Join(elems []string, sep string) string
1.5.4 func Split(s, sep string) []string
1.5.5 func SplitAfterN(s, sep string, n int) []string
1.6 索引查找
1.6.1 func Index(s, substr string) int
1.6.2 func LastIndex(s, substr string) int
1.6.3 func IndexByte(s string, c byte) int
1.6.4 func IndexRune(s string, r rune) int
1.6.5 func IndexFunc(s string, f func(rune) bool) int
1.7 修剪操作
1.7.1 func Trim(s, cutset string) string
1.7.2 func TrimSpace(s string) string
1.7.3 func TrimPrefix(s, prefix string) string
1.7.4 func TrimSuffix(s, suffix string) string
1.7.5 func TrimFunc(s string, f func(rune) bool) string
1.8 映射与替换
1.8.1 func Map(mapping func(rune) rune, s string) string
1.8.2 func Replace(s, old, new string, n int) string
1.8.3 func ReplaceAll(s, old, new string) string
1.8.4 func ToValidUTF8(s, replacement string) string
1.9 重复与迭代
1.9.1 func Repeat(s string, count int) string
1.9.2 func Lines(s string) iter.Seq[string]
1.9.3 func Fields(s string) []string
1.9.4 func FieldsFunc(s string, f func(rune) bool) []string
1.9.5 func FieldsSeq(s string) iter.Seq[string]
1.9.6 func FieldsFuncSeq(s string, f func(rune) bool) iter.Seq[string] (Go 1.22+)
2 类型
2.1 Builder
2.1.1 Cap() int - 获取底层容量
2.1.2 Grow(n int) - 预分配内存
2.1.3 Len() int - 获取内容长度
2.1.4 Reset() - 清空内容
2.1.5 String() string - 获取最终字符串
2.1.6 Write(p []byte) (int, error)
2.1.7 WriteByte(c byte) error
2.1.8 WriteRune(r rune) (int, error)
2.1.9 WriteString(s string) (int, error)
2.2 Reader
2.2.1 func NewReader(s string) *Reader
2.2.2 func (r *Reader) Len() int
2.2.3 func (r *Reader) Size() int64
2.2.4 func (r *Reader) Read(b []byte) (n int, err error)
2.2.5 func (r *Reader) ReadByte() (byte, error)
2.2.6 func (r *Reader) ReadRune() (ch rune, size int, err error)
2.2.7 func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
2.2.8 func (r *Reader) Seek(offset int64, whence int) (int64, error)
2.2.9 func (r *Reader) UnreadByte() error
2.2.10 func (r *Reader) UnreadRune() error
2.2.11 func (r *Reader) WriteTo(w io.Writer) (n int64, err error)
2.2.12 func (r *Reader) Reset(s string)
2.3 Replacer
2.3.1 func NewReplacer(oldnew ...string) *Replacer
2.3.2 func (r *Replacer) Replace(s string) string
2.3.3 func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)
最后更新于