随心的博客

好记性不如个烂笔头,随心记录!

返回顶部

常用字符串操作函数

前言:

Go 语言对字符串的操作主要集中在 strings 包中

https://go-zh.org/pkg/strings/

 正文:

判断字符串是否包含某个字符

func Contains(s, substr string)   bool  

示例:

var str = "hello world !"
exist1 := strings.Contains(str,"hello")
fmt.Println(exist1)  //输出 true

exist2 := strings.Contains(str,"golang")
fmt.Println(exist2) //输出 false

查找子串 在字符串s中第一次出现的位置

func Index(s, sep string) int

查找子串 在字符串最后出现的位置

func LastIndex(s, sep string) int

示例:

var str = "hello golang , go go golang !"

pos1 := strings.Index(str,"go")

fmt.Println(pos1)  //输出 6

pos2 := strings.LastIndex(str,"go")

fmt.Println(pos2) //输出 21

 

查找子串在字符串出现的次数

func Count(s, sep string) int

示例:

var str = "hello golang , go go golang !"

count := strings.Count(str,"go")

fmt.Println(count)  //输出 4  表示出现了4go

 

strLen := strings.Count(str,"")  //当子串为空时,表示统计字符串长度

fmt.Println(strLen)  //输出 30 ,表示共有30个字符的长度

 

将字符串的小写转换成大写

func ToUpper(s string) string

将字符串的大写转换成小写

func ToLower(s string) string

示例:

var str = "hello golang , go go golang !"

upStr := strings.ToUpper(str)

fmt.Println(upStr)  //输出 HELLO GOLANG , GO GO GOLANG !

 

loStr := strings.ToLower(upStr)

fmt.Println(loStr)  //输出 hello golang , go go golang !

 

删除字符串s左右两边连续包含cutset的字符

func Trim(s string, cutset string) string

删除字符串s左边连续包含cutset的字符

func TrimLeft(s string, cutset string) string

删除字符串s右边连续包含cutset的字符

func TrimRight(s string, cutset string) string

示例:

var str = "abcd hello golang ,abcd, go go golang ! abcd"

str1 := strings.Trim(str,"abcd")

fmt.Println(str1)  //输出  hello golang ,abcd, go go golang !

 

str2 := strings.TrimLeft(str,"abcd")

fmt.Println(str2)  //输出  hello golang ,abcd, go go golang ! abcd

 

str3 := strings.TrimRight(str,"abcd")

fmt.Println(str3)  //输出 abcd hello golang ,abcd, go go golang !

 

删除字符串s左右两边连续的空白字符

func TrimSpace(s string) string

示例:

var str = "     hello golang ,abcd, go go golang !    "
str1 := strings.TrimSpace(str)
fmt.Println(str1)  //输出 hello golang ,abcd, go go golang !

 

切分字符串

func Split(s, sep string) []string

拼接字符串

func Join(a []string, sep string) string

示例:

var str = "hello golang  abcd go go golang !"

str1 := strings.Split(str," ")  //以 空格 分割字符串

fmt.Println(str1)    //输出 [hello golang  abcd go go golang !]

fmt.Printf("%T",str1) //输出 []string 表示字符串切片

 

str2 := strings.Join(str1,",")  //以,拼接字符串

fmt.Println(str2) //输出 hello,golang,,abcd,go,go,golang,!

 

字符串重复

func Repeat(s string, count int) string

示例:

var str = "hello ,"

str1 := strings.Repeat(str,5) //重复5

fmt.Println(str1)     //输出 hello ,hello ,hello ,hello ,hello ,

 

字符串替换

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

n为替换次数   -1替换所有

示例:

var str = "hello worldhello worldhello world"

str1 := strings.Replace(str,"world","golang",2) //替换2

str2 := strings.Replace(str,"world","golang",-1) // -1 替换全部

fmt.Println(str1) //输出 hello golanghello golanghello world

fmt.Println(str2) //输出 hello golanghello golanghello golang

 

字符串忽略大小写比较

func EqualFold(s, t string) bool

示例:

pass := "aA123"
newPass := "Aa123"
res := strings.EqualFold(pass, newPass)
fmt.Println(res)          //输出 true  忽略大小写比较 相等
fmt.Println(pass==newPass) //输出false  直接比较不相等

 

posted @ 2023-03-29 21:35  yangphp  阅读(28)  评论(0编辑  收藏  举报