这篇文章主要讲解了“Golang strings包常用字符串操作函数怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang strings包常用字符串操作函数怎么使用”吧!
func Contains
func Contains(s, substr string) bool
作用:判断 substr 是否是 s 的子串,例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsContains("Linux", "in")) fmtPrintln(stringsContains("Linux", "Unix")) fmtPrintln(stringsContains("Linux", "")) fmtPrintln(stringsContains("", "")) }
输出:
true
false
true
true
func HasPrefix
func HasPrefix(s, prefix string) bool
作用:字符串 s 是否以 prefix 为开头,例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsHasPrefix("Linux", "Lin")) fmtPrintln(stringsHasPrefix("Linux", "in")) fmtPrintln(stringsHasPrefix("Linux", "")) }
输出:
true
false
true
func HasSuffix
func HasSuffix(s, suffix string) bool
作用:判断字符串 s 是否以 suffix 结尾,例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsHasSuffix("Linux", "nux")) fmtPrintln(stringsHasSuffix("Linux", "ix")) fmtPrintln(stringsHasSuffix("Linux", "")) }
输出:
true
false
true
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() { fmtPrintln(stringsReplace("Linux is very very very good!", "very", "much", 2)) fmtPrintln(stringsReplace("Linux is very very very good!", "very", "much", -1)) }
输出:
Linux is much much very good!
Linux is much much much good!
func Split
func Split(s, sep string) []string
作用:返回将字符串 s 按 sep子串分割的字符串切片,sep 为空字符串时,将 s 分割为每一个 unicode 码值的字符串切片。例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsSplit("Linux, Unix, Windows, Android", ", ")) fmtPrintln(stringsSplit(" Linux is very very very good! ", " ")) }
输出:返回的是字符串数组。
[Linux Unix Windows Android]
[ Linux is very very very good! ]
func ToLower
func ToLower(s string) string
作用:返回字符串 s 中字母转小写的拷贝,例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsToLower("Linux, Unix, Windows, Android")) fmtPrintln(stringsToLower(" Linux is very very very good! ")) }
输出:
linux, unix, windows, android
linux is very very very good!
func ToUpper
func ToUpper(s string) string
作用:返回字符串 s 中字母转大写的拷贝,例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsToUpper("Linux, Unix, Windows, Android")) fmtPrintln(stringsToUpper(" Linux is very very very good! ")) }
输出:
LINUX, UNIX, WINDOWS, ANDROID
LINUX IS VERY VERY VERY GOOD!
func Repeat
func Repeat(s string, count int) string
作用:返回 count 个字符串 s 相连接的字符串,如果 count 为负数 或(len * s * count)的结果溢出,则报 panic 异常。例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsRepeat("Linux", 6)) fmtPrintln(stringsRepeat("Linux", 0)) fmtPrintln(stringsRepeat("Linux", -1)) }
输出:
LinuxLinuxLinuxLinuxLinuxLinux
panic: strings: negative Repeat count
goroutine 1 [running]:
stringsRepeat(0x4bcf3d, 0x5, 0xffffffffffffffff, 0x1, 0x1)
/usr/local/go/src/strings/stringsgo:529 +0x5e5
mainmain()
/root/goProject/src/test/maingo:11 +0x167
func Count
func Count(s, substr string) int
作用:返回字符串 s 中包含的不重叠的子串 substr 个数,如果 substr 是一个空字符串,则返回1 + s中的Unicode代码点数(Unicode 代码点:可以简单理解为一个符号便是一个代码点),例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsCount("Golang Golang Golang", "Go")) fmtPrintln(stringsCount("Golang", "")) fmtPrintln(stringsCount("Golang语言", "")) }
输出:
3
7
9
func Index
func Index(s, substr string) int
作用:返回字符串 s 中包含的第一个子串 substr 的索引,如果不存在返回 -1,例如:
package main import ( "fmt" "strings" ) func main() { fmtPrintln(stringsIndex("Golang", "lang")) fmtPrintln(stringsIndex("Golang", "Linux")) }
输出:
2
-1
func Join
func Join(elems []string, sep string) string
作用:使用 sep 作为分隔符,将elems 中的所有字符连接起来,例如:
package main import ( "fmt" "strings" ) func main() { elems := []string{"I", "like", "golang", "!"} fmtPrintln(stringsJoin(elems, " ")) elems = []string{"123", "456", "789"} fmtPrintln(stringsJoin(elems, "-")) }
输出:
[root@localhost gotest]# go run maingo
I like golang !
123-456-789
[root@localhost gotest]#
感谢各位的阅读,以上就是“Golang strings包常用字符串操作函数怎么使用”的内容了,经过本文的学习后,相信大家对Golang strings包常用字符串操作函数怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!