ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 语法 ### Func ``` // 比较 a 是否大于 b func Compare(a, b string) int func Contains(s, substr string) bool // chars 中包含任何一个char 都算 func ContainsAny(s, chars string) bool func ContainsRune(s string, r rune) bool // substr 出现的次数 func Count(s, substr string) int // 判断两个utf-8编码字符串(将unicode大写、小写、标题三种格式字符视为相同)是否相同 func EqualFold(s, t string) bool // 切割空格 func Fields(s string) []string func FieldsFunc(s string, f func(rune) bool) []string // 匹配前缀或后缀 func HasPrefix(s, prefix string) bool func HasSuffix(s, suffix string) bool // 首次出现的位置 func Index(s, substr string) int func IndexAny(s, chars string) int func IndexByte(s string, c byte) int func IndexFunc(s string, f func(rune) bool) int func IndexRune(s string, r rune) int // 最后一次出现的位置 func LastIndex(s, substr string) int func LastIndexAny(s, chars string) int func LastIndexByte(s string, c byte) int func LastIndexFunc(s string, f func(rune) bool) int // 切片转字符串 func Join(elems []string, sep string) string func Map(mapping func(rune) rune, s string) string // 重复生成字符 func Repeat(s string, count int) string // 替换字符串 func Replace(s, old, new string, n int) string func ReplaceAll(s, old, new string) string // 切割字符串转数组 func Split(s, sep string) []string // 如 "a,b,c" -> ["a,","b,","c"] func SplitAfter(s, sep string) []string func SplitAfterN(s, sep string, n int) []string // n 表示生成几个切片 func SplitN(s, sep string, n int) []string // 首字母大写 func Title(s string) string func ToLower(s string) string func ToLowerSpecial(c unicode.SpecialCase, s string) string // 多数情况与 ToUpper 相同但在处理某些unicode编码字符则不同 func ToTitle(s string) string func ToTitleSpecial(c unicode.SpecialCase, s string) string func ToUpper(s string) string func ToUpperSpecial(c unicode.SpecialCase, s string) string func ToValidUTF8(s, replacement string) string func Trim(s, cutset string) string func TrimFunc(s string, f func(rune) bool) string func TrimLeft(s, cutset string) string func TrimLeftFunc(s string, f func(rune) bool) string func TrimPrefix(s, prefix string) string func TrimRight(s, cutset string) string func TrimRightFunc(s string, f func(rune) bool) string func TrimSpace(s string) string ``` ### Type ``` // 实现 Writer 接口 type Builder func (b *Builder) Cap() int func (b *Builder) Grow(n int) func (b *Builder) Len() int func (b *Builder) Reset() func (b *Builder) String() string func (b *Builder) Write(p []byte) (int, error) func (b *Builder) WriteByte(c byte) error func (b *Builder) WriteRune(r rune) (int, error) func (b *Builder) WriteString(s string) (int, error) // 实现 reader 接口 type Reader func NewReader(s string) *Reader func (r *Reader) Len() int func (r *Reader) Read(b []byte) (n int, err error) func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) func (r *Reader) ReadByte() (byte, error) func (r *Reader) ReadRune() (ch rune, size int, err error) func (r *Reader) Reset(s string) func (r *Reader) Seek(offset int64, whence int) (int64, error) func (r *Reader) Size() int64 func (r *Reader) UnreadByte() error func (r *Reader) UnreadRune() error func (r *Reader) WriteTo(w io.Writer) (n int64, err error) type Replacer func NewReplacer(oldnew ...string) *Replacer func (r *Replacer) Replace(s string) string func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error) ``` ## 实例 ### strings.Fields 切割并过滤多余空格 ``` s := " fooasd ads asd bar baz " fmt.Printf("%+v\n", strings.Split(s, " ")) // [ fooasd ads asd bar baz ] fmt.Printf("%+v\n", strings.Fields(s)) // ["fooasd" "ads" "asd" "bar" "baz"] ``` ### strings.FieldsFunc ``` s := "ab*cd*ef" result := strings.FieldsFunc(s, func(c rune) bool { return c == '*' }) fmt.Printf("%+v\n", result) // [ab cd ef] ``` ### strings.Map ``` res := strings.Map(func(r rune) rune { return r + 1 }, "abc 好") fmt.Printf("%+v\n", res) // bcd!奾 ``` ### strings.TrimFunc ``` s := strings.TrimFunc("ab a ca", func(r rune) bool { return r == 'a' }) fmt.Printf("%+v\n", s) // b a c ``` ### NewReplacer 声明替换规则.并使用此模式进行替换 ``` rep := strings.NewReplacer("<", "&lt", ">", "&gt") fmt.Printf("%+v\n", rep.Replace("This is <b>HTML</b>!")) // This is &ltb&gtHTML&lt/b&gt! ``` ### strings.NewReader() ``` reader := strings.NewReader("你好abc") p := make([]byte, 6) fmt.Println(reader.Read(p)) //你好 fmt.Println(reader.Len()) //3 fmt.Println(reader.Size()) //9 fmt.Printf("%s", p) ```