目录

1. func Contains

2. func HasPrefix

3. func HasSuffix

4. func Replace

5. func Split

6. func ToLower

7. func ToUpper

8. func Repeat

在编写代码的时候最常用到的就是字符串了,本篇文章将结合实例对常用的字符串操作函数进行介绍。

1. func Contains

func Contains(s, substr string) bool

判断 substr 是否是 s 的子串,例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Contains("Linux", "in"))
	fmt.Println(strings.Contains("Linux", "Unix"))
	fmt.Println(strings.Contains("Linux", ""))
	fmt.Println(strings.Contains("", ""))
}

输出:

true
false
true
true

2. func HasPrefix

func HasPrefix(s, prefix string) bool

字符串 s 是否以 prefix 为开头,例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.HasPrefix("Linux", "Lin"))
	fmt.Println(strings.HasPrefix("Linux", "in"))
	fmt.Println(strings.HasPrefix("Linux", ""))
}

输出:

true
false
true

3. func HasSuffix

func HasSuffix(s, suffix string) bool

判断字符串 s 是否以 suffix 结尾,例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.HasSuffix("Linux", "nux"))
	fmt.Println(strings.HasSuffix("Linux", "ix"))
	fmt.Println(strings.HasSuffix("Linux", ""))
}

输出为:

true
false
true

4. func Replace

func Replace(s, old, new string, n int) string

返回 s 中前 n 个不重复的 old 子串替换为 new 子串的新字符串,如果 n < 0 ,则替换所有 old 子串,例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Replace("Linux is very very very good!", "very", "much", 2))
	fmt.Println(strings.Replace("Linux is very very very good!", "very", "much", -1))
}

输出为:

Linux is much much very good!
Linux is much much much good!

5. func Split

func Split(s, sep string) []string

返回将字符串 s 按 sep子串分割的字符串切片,sep 为空字符串时,将 s 分割为每一个 unicode 码值的字符串切片。例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Split("Linux, Unix, Windows, Android", ", "))
	fmt.Println(strings.Split(" Linux is very very very good! ", " "))
}

输出为:返回的是字符串数组。

[Linux Unix Windows Android]
[ Linux is very very very good! ]

6. func ToLower

func ToLower(s string) string

返回字符串 s 中字母转小写的拷贝,例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToLower("Linux, Unix, Windows, Android"))
	fmt.Println(strings.ToLower(" Linux is very very very good! "))
}

输出为:

linux, unix, windows, android
 linux is very very very good!

7. func ToUpper

func ToUpper(s string) string

返回字符串 s 中字母转大写的拷贝,例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToUpper("Linux, Unix, Windows, Android"))
	fmt.Println(strings.ToUpper(" Linux is very very very good! "))
}

输出为:

LINUX, UNIX, WINDOWS, ANDROID
 LINUX IS VERY VERY VERY GOOD! 

8. func Repeat

func Repeat(s string, count int) string

返回 count 个字符串 s 相连接的字符串,如果 count 为负数 或(len * s * count)的结果溢出,则报 panic 异常。例如:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Repeat("Linux", 6))
	fmt.Println(strings.Repeat("Linux", 0))
	fmt.Println(strings.Repeat("Linux", -1))
}

输出为:

LinuxLinuxLinuxLinuxLinuxLinux

panic: strings: negative Repeat count

goroutine 1 [running]:
strings.Repeat(0x4bcf3d, 0x5, 0xffffffffffffffff, 0x1, 0x1)
        /usr/local/go/src/strings/strings.go:529 +0x5e5
main.main()
        /root/goProject/src/test/main.go:11 +0x167

参考链接:

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