Go中常用包笔记 字符串strings(四)

2023-04-20,,

Package strings

此包和bytes包十分类似,除了没有bytes.Buffer,func Equal(a, b []byte) bool ,func Runes(s []byte) []rune,以外,bytes包含的方法strings都含有,只不过各方法其中一个参数有[]byte变为string而已.但是有个独特的type Replacer:

func NewReplacer(oldnew ...string) *Replacer //构造函数,不定参数,参数格式为old1,new1,old2,new2,old3,new3......表示old1会被替换为new1,old2会被替换为new2...

    func (r *Replacer) Replace(s string) string //进行替换 返回新的字符串

    func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error) //替换后并写入io.Writer

其他和bytes包类似功能的方法:

字符串和字符串的关系(逐字节比较,相等,相似,包含,出现的位置,包含次数,是否是前缀后缀)

func Compare(a, b string) int

  

   func EqualFold(s, t string) bool

func Contains(s, substr string) bool

func Index(s, sep string) int

func LastIndex(s, sep string) int

func Count(s, sep string) int

func HasPrefix(s, prefix string) bool

func HasSuffix(s, suffix string) bool

 

字符串和字符串的关系(是否包含串里任何一个rune以及在字符串中的位置)

 

   func ContainsAny(s, chars string) bool

   func IndexAny(s, chars string) int

func LastIndexAny(s, chars string) int

 

字符串和rune的关系(包含,转换)

func ContainsRune(s string, r rune) bool

func IndexFunc(s string, f func(rune) bool) int

func IndexRune(s string, r rune) int

func LastIndexFunc(s string, f func(rune) bool) int

 

字符数组和字节的关系(位置,)

func IndexByte(s string, c byte) int

func LastIndexByte(s string, c byte) int

 

操作字符串(使用空格或者符合条件的rune分割分组,将分组连接成字符串,分割,大小写转换,修剪两端,按规则修改包含的每个rune,重复,替换)

 

func Fields(s string) []string

func FieldsFunc(s string, f func(rune) bool) []string

func Join(a []string, sep string) string

func Split(s, sep string) []string

func SplitAfter(s, sep string) []string

func SplitAfterN(s, sep string, n int) []string

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

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 Trim(s string, cutset string) string

func TrimFunc(s string, f func(rune) bool) string

func TrimLeft(s string, cutset string) string

func TrimLeftFunc(s string, f func(rune) bool) string

func TrimPrefix(s, prefix string) string

func TrimRight(s string, cutset string) string

func TrimRightFunc(s string, f func(rune) bool) string

func TrimSpace(s string) string

func TrimSuffix(s, suffix 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


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)

《Go中常用包笔记 字符串strings(四).doc》

下载本文的Word格式文档,以方便收藏与打印。