Golang strings包常用字符串操作函数
在编写代码的时候最常用到的就是字符串了,golang 中的字符串统一使用 utf-8 (属于unicode编码的一种实现方式)进行编码,本篇文章将结合具体实例对常用的字符串操作函数进行介绍。
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包字符串操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!