在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
| n, err := strconv.Atoi("12") if err != nil { fmt.Println("转换失败") } fmt.Println(n)
int64, err := strconv.ParseInt(string, 10, 64)
string:=strconv.Itoa(int)
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
| fmt.Println("Join: ", strings.Join([]string{"a", "b", "c"}, "-"))
fmt.Println("Split: ", strings.Split("a-b-c-d-e", "-"))
func Fields(s string) []string
str := "nice to meet you" slice := strings.Fields(str)
|
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"))) fmt.Println(strings.Compare(string("hello"), string("helloworld"))) fmt.Println(strings.Compare(string("hello"), string("hello")))
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
| func Contains(s, substr string) bool
fmt.Println(strings.Contains("hello word", "hell")) fmt.Println(strings.Contains("hello word", "hella")) fmt.Println(strings.Contains("", ""))
func Index(s, sep string) int
func LastIndex(s, sep string) int
fmt.Println(strings.Index("abcdefga", "a")) fmt.Println(strings.LastIndex("abcdefga", "a"))
func Count(s, sep string) int
fmt.Println(strings.Count("hello", "l")) fmt.Println(strings.Count("hello", ""))
|
2.4 字符串重复
1 2 3 4
| func Repeat(s string, count int) string
fmt.Println(strings.Repeat("hello", 2))
|
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"))
func ToLower(s string) string
fmt.Println(strings.ToLower("HELLO"))
func ToUpper(s string) string
fmt.Println(strings.ToUpper("hello"))
|
2.6 字符串前缀后缀
大小写敏感
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| func HasPrefix(s, prefix string) bool
fmt.Println(strings.HasPrefix("Golang", "Go")) fmt.Println(strings.HasPrefix("Golang", "go")) fmt.Println(strings.HasPrefix("Golang", "a")) fmt.Println(strings.HasPrefix("Golang", ""))
func HasSuffix(s, suffix string) bool
fmt.Println(strings.HasSuffix("Golang", "ang")) fmt.Println(strings.HasSuffix("Golang", "Ang")) fmt.Println(strings.HasSuffix("Golang", "bng")) fmt.Println(strings.HasSuffix("Golang", ""))
|
2.7 字符串删除
1 2 3 4
| func Trim(s string, cutset string) string
fmt.Println(strings.Trim("@hello word@","@"))
|
2.8 字符串替换
1 2 3 4 5 6
| func Replace(s, old, new string, n int) string
fmt.Println(strings.Replace("hello word","o","@",1)) fmt.Println(strings.Replace("hello word","o","@",-1))
|