在GO中字符串是用双引号(“”)或反引号(``)括起来,而且字符串不可变

1 字符串操作

1.1 修改字符串

直接修改字符串报错,如下代码:

1
2
3
4
5
6
var s string = "hello word"
s[0] =`a`

//报错
# command-line-arguments
...cannot assign to str[0]

通过转[]byte类型修改字符串

1
2
3
4
5
var s string = "hello word"
slice := []byte(s)
slice[0] ='H'
newStr := string(slice)
fmt.Println(newStr)

此处的 slice[0] =’H’ 不能写成 slice[0] =”H”,否则会报错:cannot use “H” (type string) as type byte in assignment

1.2 字符串转[]byte 类型

1
2
var s string = "hello word"
sic := []byte(s)

1.3 字符串转整型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// string到int
n, err := strconv.Atoi("12")
if err != nil {
fmt.Println("转换失败")
}
fmt.Println(n)

// string到int64
int64, err := strconv.ParseInt(string, 10, 64)

// int到string
string:=strconv.Itoa(int)

//int64到string
string:=strconv.FormatInt(int64,10)

1.4 字符串连接

1
2
3
4
//字符串连接
a, b := "hello", " word"
c := a + b
fmt.Println(c)

1.5 声明多行字符串

1
2
str := ` hello,
nice to meet you`

`` 括起的字符串为Raw字符串,即字符串在代码中的形式就是打印时的形式,它没有字符转义,换行也将原样输出。

2 字符串常用函数

处理字符串的函数主要集中在strings和strconv

2.1 字符串拼接和分割

使用函数:strings.Join 和 strings.Split

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//切片转 以x拼接成 字符串
fmt.Println("Join: ", strings.Join([]string{"a", "b", "c"}, "-"))
//输出:Join: a-b-c

// 字符串 以x分割成 切片
fmt.Println("Split: ", strings.Split("a-b-c-d-e", "-"))
//输出:Split: [a b c d e]


// 去除字符串s中的空格符, 并按照空格(可以是一个或者多个空格)分割字符串, 返回slice
func Fields(s string) []string

str := "nice to meet you"
slice := strings.Fields(str) // [nice to meet you]

2.2 字符串比较

使用函数:strings.Compare

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

fmt.Println(strings.Compare(string("hello"), string("haha"))) // 1
fmt.Println(strings.Compare(string("hello"), string("helloworld"))) // -1
fmt.Println(strings.Compare(string("hello"), string("hello"))) //0

// Compare函数:源码
func Compare(a, b string) int {
if a == b {
return 0
}
if a < b {
return -1
}
return +1
}

2.3 字符串查找

使用函数:strings.Contains、Index、LastIndex、Count

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 判断给定字符串s中是否包含子串substr, 找到返回true, 找不到返回false
func Contains(s, substr string) bool

fmt.Println(strings.Contains("hello word", "hell")) // true
fmt.Println(strings.Contains("hello word", "hella")) // false
fmt.Println(strings.Contains("", "")) // true

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

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

fmt.Println(strings.Index("abcdefga", "a")) //0
fmt.Println(strings.LastIndex("abcdefga", "a"))//7


// 统计给定子串sep的出现次数, sep为空时, 返回1 + 字符串的长度
func Count(s, sep string) int

fmt.Println(strings.Count("hello", "l"))//2
fmt.Println(strings.Count("hello", ""))//6

2.4 字符串重复

1
2
3
4
// 重复s字符串count次, 最后返回新生成的重复的字符串
func Repeat(s string, count int) string

fmt.Println(strings.Repeat("hello", 2)) //hellohello

2.5 字符串大小写转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 首字母大写
func Title(s string) string

fmt.Println(strings.Title("hello")) //Hello


// 所有字母转换为小写
func ToLower(s string) string

fmt.Println(strings.ToLower("HELLO")) //hello


// 所有字母转换为大写
func ToUpper(s string) string

fmt.Println(strings.ToUpper("hello")) //HELLO

2.6 字符串前缀后缀

大小写敏感

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 判断字符串是否包含前缀prefix
func HasPrefix(s, prefix string) bool

fmt.Println(strings.HasPrefix("Golang", "Go")) // true
fmt.Println(strings.HasPrefix("Golang", "go")) // false
fmt.Println(strings.HasPrefix("Golang", "a")) // false
fmt.Println(strings.HasPrefix("Golang", "")) // true


// 判断字符串是否包含后缀suffix,
func HasSuffix(s, suffix string) bool

fmt.Println(strings.HasSuffix("Golang", "ang")) // true
fmt.Println(strings.HasSuffix("Golang", "Ang")) // false
fmt.Println(strings.HasSuffix("Golang", "bng")) // false
fmt.Println(strings.HasSuffix("Golang", "")) // true

2.7 字符串删除

1
2
3
4
// 删除在s字符串的头部和尾部中由cutset指定的字符, 并返回删除后的字符串
func Trim(s string, cutset string) string

fmt.Println(strings.Trim("@hello word@","@")) // hello word

2.8 字符串替换

1
2
3
4
5
6
// 在s字符串中, 把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
func Replace(s, old, new string, n int) string


fmt.Println(strings.Replace("hello word","o","@",1)) // hell@ word
fmt.Println(strings.Replace("hello word","o","@",-1)) // hell@ w@rd