Fields

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

Fields:使用空白分割字符串。
FieldsFunc:根据传入的函数分割字符串,如果当前参数c不是数字或者字母,返回true作为分割符号。

1
2
3
4
5
6
fmt.Println(strings.Fields(" foo bar baz ")) // ["foo" "bar" "baz"]
f := func(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}
fmt.Println(strings.FieldsFunc(" foo1;bar2,baz3...", f)) // ["foo1" "bar2" "baz3"]

HasPrefix 和 HasSuffix

判断字符串是否是以某个子串作为开头或者结尾。(注:可以理解为js中startsWith和endsWith)

1
2
strings.HasPrefix("hello", "he") // true
strings.HasSuffix("hello", "lo") // true

join

使用某个sep,连接字符串。

1
2
s := []string{"foo", "bar", "baz"}
strings.Join(s, ", ") // foo,bar,baz

Index,IndexAny,IndexByte,IndexFunc,IndexRune

都是返回满足条件的第一个位置,如果没有满足条件的数据,返回-1。

1
2
3
// Index
strings.Index("hello", "lo") // 3
strings.Index("hello", "ab") // -1

LastIndex,LastIndexAny,LastIndexByte和LastIndexFunc

与 Index 用法保持一致,从右往前计数。

1
2
3
strings.LastIndex("hello", "h") // 0
fmt.Println(strings.LastIndex("hello", "el")) // 1
strings.LastIndex("hello", "ab") // -1

Contains,ContainsAny 和 ContainsRune

字符串s中是否包含substr,返回true或者false。

1
2
strings.Contains("hello", "he") // true
strings.Contains("hello", "ab") // false

Count

判断子串在源串中的数量,如果子串为空,则长度为源串的长度+1。

1
2
strings.Count("hello", "l") // 2
strings.Count("hello", "") // 6 = 5 + 1

EqualFold

在不区分大小写的情况下,判断两个字符串是否相同。

1
strings.EqualFold("hello", "HeLLo") // true

Repeat

重复一下s,count是重复的次数,不能传负数。

1
strings.Repeat("hello", 2) // hellohello

Replace和ReplaceAll

使用 new 来替换 old ,替换的次数为 n。如果n为负数,则替换所有的满足条件的子串。

1
2
3
strings.Replace("hello world", "o", "oo", 1) // "helloo world"
strings.Replace("hello world", "o", "oo", -1) // "helloo woorld"

ReplaceAll 使用new替换所有的old,相当于使用Replace时 n<0。

1
strings.ReplaceAll("hello world", "o", "oo") // "helloo woorld"

Split,SplitN,SplitAfter和SplitAfterN

将字符串切割成数组。

1
2
3
4
5
6
strings.Split("a,b,c", ",") // ["a","b","c"]
strings.Split("a man a plan a canal panama", "a ") // ["" "man " "plan " "canal panama"]
// SplitN 定义返回之后的切片中包含的长度,最后一部分是未被处理的。
strings.SplitN("a,b,c", ",", 2)) // ["a", "b,c"]

Trim,TrimFunc,TrimLeft,TrimLeftFunc,TrimPrefix,TrimSuffix,TrimRight,TrimRightFunc

1
2
3
4
5
6
7
8
9
10
11
// Trim 包含在cutset中的元素都会被去掉
strings.Trim("¡¡¡Hello, Gophers!!!", "!¡") // Hello, Gophers
// TrimLeft 去掉左边满足包含在cutset中的元素,直到遇到不在cutset中的元素为止
strings.TrimLeft(" Hello, Gophers", "! ") // Hello, Gophers
strings.TrimRight("Hello, Gophers ", "! ") // Hello, Gophers
// TrimPrefix 去掉开头部分;TrimSuffix 去掉结尾部分
var s = "¡¡¡Hello, Gophers!!!"
s = strings.TrimPrefix(s, "¡¡¡Hello, ") // Gophers!!!
s = strings.TrimPrefix(s, "Gophers!!!") // ¡¡¡Hello,

strings.Builder

strings.Builder 使用 Write 方法来高效的构建字符串。它最小化了内存拷贝,耗费零内存,不要拷贝非零的Builder。

1
2
3
4
5
6
var b strings.Builder
for i := 3; i >= 1; i-- {
fmt.Fprintf(&b, "%d...", i)
}
b.WriteString("ignition")
fmt.Println(b.String()) // 3...2...1...ignition
1
2
3
4
5
6
7
8
9
func (b *Builder) Cap() int // 容量,涉及批量内存分配机制
func (b *Builder) Grow(n int) // 手动分配内存数量
func (b *Builder) Len() int // 当前builder中含有的所有字符长度
func (b *Builder) Reset() // 清空builder
func (b *Builder) String() string // 转化为字符串输出
func (b *Builder) Write(p []byte) (int, error) // 往builder写入数据
func (b *Builder) WriteByte(c byte) error // 往builder写入数据
func (b *Builder) WriteRune(r rune) (int, error) // 往builder写入数据
func (b *Builder) WriteString(s string) (int, error) // 往builder写入数据

strings.Reader

Reader 通过读取字符串的方式,实现了接口 io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, io.ByteScannerio.RuneScanner。零值Reader操作起来就像操作空字符串的io.Reader一样。

1
2
3
4
5
type Reader struct {
s string //对应的字符串
i int64 // 当前读取到的位置
prevRune int
}
1
2
3
4
5
6
7
8
9
10
11
12
func NewReader(s string) *Reader // 初始化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) // 重置以从s中读
func (r *Reader) Seek(offset int64, whence int) (int64, error) // Seek implements the io.Seeker interface.
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) // WriteTo implements the io.WriterTo interface.

Len,Size,Read

  • Len: 返回未读的字符串长度。
  • Size: 返回字符串的长度。
  • Read: 读取字符串信息,读取之后会改变Len的返回值
1
2
3
4
5
6
7
8
9
10
11
12
r := strings.NewReader("abcdefghijklmn")
fmt.Println(r.Len()) // 输出14 初始时,未读长度等于字符串长度
var buf []byte
buf = make([]byte, 5)
readLen, err := r.Read(buf)
fmt.Println("读取到的长度:", readLen) //读取到的长度5
if err != nil {
fmt.Println("错误:", err)
}
fmt.Println(buf) //adcde
fmt.Println(r.Len()) //9 读取到了5个 剩余未读是14-5
fmt.Println(r.Size()) //14 字符串的长度

ReadAt

读取偏移off字节后的剩余信息到b中,ReadAt函数不会影响Len的数值。

1
2
3
4
5
6
7
8
9
10
11
12
13
r := strings.NewReader("abcdefghijklmn")
var bufAt, buf []byte
buf = make([]byte, 5)
r.Read(buf)
fmt.Println("剩余未读的长度", r.Len()) //剩余未读的长度 9
fmt.Println("已读取的内容", string(buf)) //已读取的内容 abcde
bufAt = make([]byte, 256)
r.ReadAt(bufAt, 5)
fmt.Println(string(bufAt)) //fghijklmn
//测试下是否影响Len和Read方法
fmt.Println("剩余未读的长度", r.Len()) //剩余未读的长度 9
fmt.Println("已读取的内容", string(buf)) //已读取的内容 abcde

字符串反转

1
2
3
4
5
6
7
8
// 反转字符串
func reverseString(s string) string {
runes := []rune(s)
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}