go语言strings包中的方法说明

go语言strings包的方法使用


go语言中的strings包主要是对字符串的操作,中定义了一系列对字符串的操作

/*
	strings包中的方法使用
*/

//contains函数:查找子字符串中是否在指定的字符串
	fmt.Println("1、contains使用例子:")
	fmt.Println(strings.Contains("hello world","he"))
	//containsAny:判断参数一是否有参数二中的任意字符
	fmt.Println("2、containsAny使用例子:")
	fmt.Println(strings.ContainsAny("hello world","ze"))
	//containsRune:判断参数一是否含有utf-8码值 参数二
	fmt.Println("3、containsRune使用例子:")
	fmt.Println(strings.ContainsRune("我爱中国",'你'))
	//EqualFold:判断两个utf-8的字符串是否相同
	fmt.Println("4、EqualFold使用例子:")
	fmt.Println(strings.EqualFold("中国","你好"))
	//HasPrefix:判断参数一的前缀是否有参数二字符串
	fmt.Println("5、HasPrefix使用例子:")
	fmt.Println(strings.HasPrefix("hello world","hello"))
	//hasSuffix:判断参数一的后缀是否含有参数二的字符串
	fmt.Println("6、hasSuffix使用例子:")
	fmt.Println(strings.HasSuffix("hello world","world"))
	//Count:返回参数一中有多少个不重复的参数二
	fmt.Println("7、Count使用例子:")
	fmt.Println(strings.Count("hello world","l"))
	//Index:返回参数二第一次出现在参数一的位置
	fmt.Println("8、Index的使用例子:")
	fmt.Println(strings.Index("hello world","l"))
	//IndexByte:参数二字符第一次出现在参数一中的位置
	fmt.Println("9、IndexByte的使用例子")
	fmt.Println(strings.Index("hello world","l"))
	//IndexAny:参数二字符串中的utf-8码值在参数一第一次出现的位置
	fmt.Println("10、IndexAny的使用例子:")
	fmt.Println(strings.IndexAny("我是中国人","中"))
	//LastIndex:参数二字符串最后一次出现的位置
	fmt.Println("11、LastIndex的使用例子:")
	fmt.Println(strings.LastIndex("hello world","l"))
	//LastIndexAny:utf-8形式的参数二最后一次出现在参数一的位置
	fmt.Println("12、LastIndexAny的使用例子:")
	fmt.Println(strings.LastIndexAny("你好,我来自中国,你呢","你"))
	//Title:返回参数中的每个单词的首字母都改为标题格式的字符串拷贝
	fmt.Println("13、Title的使用例子")
	fmt.Println(strings.Title("hello world"))
	//ToLower:返回将所有的字母都转为对应的小写版本
	fmt.Println("14、ToLower使用例子:")
	fmt.Println(strings.ToLower("HELLO WORLD"))
	//ToUpper:返回值将所有字母都转为对应的大写版本
	fmt.Println("15、ToUpper使用例子:")
	fmt.Println(strings.ToUpper("hello world"))
	//ToTitle:将所有字母都转为对应的标题版本的拷贝
	fmt.Println("16、ToTitle的使用例子:")
	fmt.Println(strings.ToTitle("hello"))
	//Repeat:返回参数二个参数一的串联的字符串
	fmt.Println("17、Repeat的使用例子:")
	fmt.Println(strings.Repeat("hello ",3))
	//Replace: func Replace(s, old, new string, n int)
	//返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串
	fmt.Println("18、Repeat的使用例子")
	fmt.Println(strings.Replace("hello world","l","c",-1))
	//Trim:返回将参数一中前后端所有的参数二包含的utf-8码值都去掉的字符串
	fmt.Println("19、Trim的使用例子:")
	fmt.Println(strings.Trim("你好,我叫xxx,我待会联系你","你"))
	//TrimLeft:返回将参数一前端所有参数二的包含的utf-8码值去掉的字符串
	fmt.Println("20、TrimLeft的使用例子:")
	fmt.Println(strings.TrimLeft("我是中国人","我是"))
	//TrimRight:将参数一后端所有的参数二包含的utf-8码值去掉
	fmt.Println("21、TrimRight使用例子:")
	fmt.Println(strings.TrimRight("你好张三","张三"))
	//TrimSuffix:返回去除参数一中的后缀参数二的字符串
	fmt.Println("22、TrimSuffix的使用例子:")
	fmt.Println(strings.TrimSuffix("a1.txt",".txt"))
	//Fields:将字符串按照空白分割成若干个字符串
	fmt.Println("23、Fields的使用例子:")
	fmt.Println(strings.Fields("hello my name is"))
	//Split:参数一以参数二为分隔符进行分割,返回生成的切片
	fmt.Println("24、Split的使用例子:")
	fmt.Println(strings.Split("hello?my?name?is","?"))
	//Join:将一系列的字符串连接成一个字符串,中间用参数二来分隔
	fmt.Println("25、Join的使用例子:")
	fmt.Println(strings.Join([]string{"hello","my","name"}," "))

因为太懒就不放效果图了,大家有兴趣可以参考一下

参考链接:https://studygolang.com/pkgdoc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值