Go 标准库介绍一: strings - Go语言中文社区

Go 标准库介绍一: strings


原文链接 http://ironxu.com/740

本文介绍Go 标准库 strings 常用导出函数,结构体及其方法。

importstrings

strings 包实现了用于操作字符串的简单函数,包括 strings 导出函数和 Reader, Replacer 两个结构体。

1. strings 常用导出函数

判断字符串与子串关系

func EqualFold(s, t string) bool // 判断两个utf-8编码字符串,大小写不敏感

func HasPrefix(s, prefix string) bool // 判断s是否有前缀字符串prefix

func Contains(s, substr string) bool // 判断字符串s是否包含子串substr

func ContainsAny(s, chars string) bool // 判断字符串s是否包含字符串chars中的任一字符

func Count(s, sep string) int // 返回字符串s中有几个不重复的sep子串

获取字符串中子串位置

func Index(s, sep string) int // 子串sep在字符串s中第一次出现的位置,不存在则返回-1

func IndexByte(s string, c byte) int // 字符c在s中第一次出现的位置,不存在则返回-

func IndexAny(s, chars string) int // 字符串chars中的任一utf-8码值在s中第一次出现的位置,如果不存在或者chars为空字符串则返回-1

func IndexFunc(s string, f func(rune) bool) int // s中第一个满足函数f的位置i(该处的utf-8码值r满足f(r)==true),不存在则返回-1

func LastIndex(s, sep string) int // 子串sep在字符串s中最后一次出现的位置,不存在则返回-1

字符串中字符处理

func Title(s string) string // 返回s中每个单词的首字母都改为标题格式的字符串拷贝

func ToLower(s string) string // 返回将所有字母都转为对应的小写版本的拷贝

func ToUpper(s string) string // 返回将所有字母都转为对应的大写版本的拷贝

func Repeat(s string, count int) string // 返回count个s串联的字符串

func Replace(s, old, new string, n int) string // 返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串

func Map(mapping func(rune) rune, s string) string // 将s的每一个unicode码值r都替换为mapping(r),返回这些新码值组成的字符串拷贝。如果mapping返回一个负值,将会丢弃该码值而不会被替换

字符串前后端处理

func Trim(s string, cutset string) string // 返回将s前后端所有cutset包含的utf-8码值都去掉的字符串

func TrimSpace(s string) string // 返回将s前后端所有空白(unicode.IsSpace指定)都去掉的字符串

func TrimFunc(s string, f func(rune) bool) string // 返回将s前后端所有满足f的unicode码值都去掉的字符串

字符串分割与合并

func Fields(s string) []string // 返回将字符串按照空白(通过unicode.IsSpace判断,可以是一到多个连续的空白字符)分割的多个字符串

func Split(s, sep string) []string // 用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片

func Join(a []string, sep string) string // 将一系列字符串连接为一个字符串,之间用sep来分隔

strings 导出函数示例

$GOPATH/src/github.com/ironxu/go_note/library/strings/strings.go 源码如下:

// go 标准库 stringspackage mainimport("fmt""strings")funcmain(){// 判断两个utf-8编码字符串,大小写不敏感s, t :="hello go","hello Go"is_equal := strings.EqualFold(s, t)    fmt.Println("EqualFold: ", is_equal)// EqualFold:  true// 判断s是否有前缀字符串prefixprefix :="hello"has_prefix := strings.HasPrefix(s, prefix)    fmt.Println(has_prefix)// true// 判断s是否有后缀字符串suffixsuffix :="go"has_suffix := strings.HasSuffix(s, suffix)    fmt.Println(has_suffix)// true// 判断字符串s是否包含子串substrsubstr :="lo"con := strings.Contains(s, substr)    fmt.Println(con)// true// 判断字符串s是否包含utf-8码值rr := rune(101)    ru :='e'con_run := strings.ContainsRune(s, r)    fmt.Println(con_run, r, ru)// true// 子串sep在字符串s中第一次出现的位置,不存在则返回-1sep :="o"sep_idnex := strings.Index(s, sep)    fmt.Println(sep_idnex)// 4// 子串sep在字符串s中最后一次出现的位置,不存在则返回-1sep_lastindex := strings.LastIndex(s, sep)    fmt.Println(sep_lastindex)// 7// 返回s中每个单词的首字母都改为标题格式的字符串拷贝title := strings.Title(s)    fmt.Println(title)// Hello Go// 返回将所有字母都转为对应的标题版本的拷贝to_title := strings.ToTitle(s)    fmt.Println(to_title)// HELLO GO// 返回将所有字母都转为对应的小写版本的拷贝s_lower := strings.ToLower(s)    fmt.Println(s_lower)// hello go// 返回count个s串联的字符串s_repeat := strings.Repeat(s,3)    fmt.Println(s_repeat)// hello gohello gohello go// 返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串s_old, s_new :="go","world"s_replace := strings.Replace(s, s_old, s_new,-1)    fmt.Println(s_replace)// hello world// 返回将s前后端所有cutset包含的utf-8码值都去掉的字符串s, cutset :="#abc!!!","#!"s_new = strings.Trim(s, cutset)    fmt.Println(s, s_new)// #abc!!! abc// 返回将字符串按照空白(unicode.IsSpace确定,可以是一到多个连续的空白字符)分割的多个字符串s ="hello world! go language"s_fields := strings.Fields(s)fork, v := range s_fields {        fmt.Println(k, v)    }// 0 hello// 1 world!// 2 go// 3 language// 用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片s_split := strings.Split(s," ")    fmt.Println(s_split)// [hello world! go language]// 将一系列字符串连接为一个字符串,之间用sep来分隔s_join := strings.Join([]string{"a","b","c"},"/")    fmt.Println(s_join)// a/b/c// 将s的每一个unicode码值r都替换为mapping(r),返回这些新码值组成的字符串拷贝。如果mapping返回一个负值,将会丢弃该码值而不会被替换map_func := func(r rune) rune {switch{caser >'A'&& r <'Z':returnr +32caser >'a'&& r <'z':returnr -32}returnr    }    s ="Hello World!"s_map := strings.Map(map_func, s)    fmt.Println(s_map)// hELLO wORLD!}

2. Reader 结构体

Reader 类型从一个字符串读取数据,实现了io.Reader, io.Seeker等接口。

func NewReader(s string) *Reader // 通过字符串 s 创建一个 Reader

func (r *Reader) Len() int // 返回 r 还没有读取部分的长度

func (r *Reader) Read(b []byte) (n int, err error) // 读取部分数据到 b 中,读取的长度取决于 b 的容量

func (r *Reader) ReadByte() (b byte, err error) // 从 r 中读取一字节数据

$GOPATH/src/github.com/ironxu/go_note/library/strings/reader.go 源码如下:

// go 标准库 strings.Readerpackage mainimport("fmt""strings")funcmain(){    s :="hello world"// 创建 Readerr := strings.NewReader(s)    fmt.Println(r)// &{hello world 0 -1}fmt.Println(r.Size())// 11 获取字符串长度fmt.Println(r.Len())// 11 获取未读取长度// 读取前6个字符forr.Len() >5{        b, err := r.ReadByte()// 读取1 bytefmt.Println(string(b), err, r.Len(), r.Size())// h 10 11// e 9 11// l 8 11// l 7 11// o 6 11//  5 11}// 读取还未被读取字符串中5字符的数据b_s := make([]byte,5)    n, err := r.Read(b_s)    fmt.Println(string(b_s), n ,err)// world 5 fmt.Println(r.Size())// 11fmt.Println(r.Len())// 0}

3. Replacer 结构体

Replacer 类型实现字符串替换的操作

func NewReplacer(oldnew ...string) *Replacer // 使用提供的多组old、new字符串对创建一个*Replacer

func (r *Replacer) Replace(s string) string // 返回s 所有替换完后的拷贝

func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error) // 向w中写入s替换完后的拷贝

$GOPATH/src/github.com/ironxu/go_note/library/strings/replace.go 源码如下:

// go 标准库 strings.Replacerpackagemainimport("fmt""strings""os")funcmain(){    s :="

Go Language

"r := strings.NewReplacer("<","<",">",">")    fmt.Println(r.Replace(s))    r.WriteString(os.Stdout, s)}

参考

pkg/strings 中文

pkg/strings

版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/bc50472e1805
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-09 21:50:50
  • 阅读 ( 924 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢