Go-strings

Packagestrings提供了对UTF-8编码字符串的一些简单操作。

安装gomacro,Go版本的REPL,对一些函数进行验证。
go get github.com/cosmos72/gomacro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
➜  hysyeah gomacro
// Welcome to gomacro. Type :help for help, :copy for copyright and license.
// This is free software with ABSOLUTELY NO WARRANTY.
gomacro> import "fmt"
gomacro> import "strings"
gomacro> strings.Compare("hello","hello")
0 // int
gomacro> strings.Compare("hello","hell")
1 // int
gomacro> strings.Compare("hell","hello")
-1 // int
gomacro> strings.Contains("hello","h")
true // bool

gomacro> strings.ContainsAny("hello","e")
true // bool
gomacro> strings.ContainsAny("hello编程","编程")
true // bool
gomacro> strings.Count("hello", "h")
1 // int
gomacro> strings.Count("hello", "l")
2 // int
gomacro> strings.HasPrefix("hello","he")
true // bool
gomacro> strings.Index("hello", "h")
0 // int
gomacro> strings.IndexAny("hello", "x") //匹配任意一个字符,并返回第一个匹配字符的index,如果不匹配则返回-1
-1 // int
gomacro> strings.IndexAny("hello","hl")
0 // int
gomacro> strings.Split("hello,a,b",",")
[hello a b] // []string
gomacro> s := []string{"foo", "bar", "baz"}
gomacro> fmt.Println(strings.Join(s, ", "))
foo, bar, baz
14 // int
<nil> // error

REF:
1.https://golang.org/pkg/strings/