当前位置: 代码网 > it编程>前端脚本>Golang > Go中strings包的基本使用示例代码

Go中strings包的基本使用示例代码

2024年11月26日 Golang 我要评论
本篇主要总结的是go中的string包的一些函数的操作讲解string在各个语言中,都有对应的处理字符串的包,在go中是使用strings来处理的前缀和后缀hasprefix() 判断字符串 s 是否

本篇主要总结的是go中的string包的一些函数的操作讲解

string

在各个语言中,都有对应的处理字符串的包,在go中是使用strings来处理的

前缀和后缀

hasprefix() 判断字符串 s 是否以 prefix 开头:

strings.hasprefix(s, prefix string) bool

hassuffix() 判断字符串 s 是否以 suffix 结尾:

strings.hassuffix(s, suffix string) bool

示例代码

func test1() {
	fmt.println(strings.hasprefix("this is string", "this"))
	fmt.println(strings.hasprefix("this is string", "1this"))
	fmt.println(strings.hassuffix("this is string", "ing"))
	fmt.println(strings.hassuffix("this is string", "iing"))
}

字符串包含

contains() 判断字符串 s 是否包含 substr:

strings.contains(s, substr string) bool

示例代码

func test2() {
	totalstring := "hello go, i love cpp"
	containstring1 := "go"
	containstring2 := "cpp"
	containstring3 := "java"
	fmt.printf("\"%s\" contain in \"%s\"? the ans is %t\n", containstring1, totalstring, strings.contains(totalstring, containstring1))
	fmt.printf("\"%s\" contain in \"%s\"? the ans is %t\n", containstring2, totalstring, strings.contains(totalstring, containstring2))
	fmt.printf("\"%s\" contain in \"%s\"? the ans is %t\n", containstring3, totalstring, strings.contains(totalstring, containstring3))
}

这里顺便温习一下对于go中转义字符的使用

判断子字符串或字符在父字符串中出现的位置

index() 返回字符串 str 在字符串 s 中的索引(str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str:

strings.index(s, str string) int

lastindex() 返回字符串 str 在字符串 s 中最后出现位置的索引(str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str:

strings.lastindex(s, str string) int

示例代码:

func test3() {
	totalstring := "hello go, i love cpp, i love cpp and go"
	containstring1 := "go"
	containstring2 := "cpp"
	containstring3 := "java"
	fmt.println("first index demo is:")
	fmt.printf("\"%s\" first index in \"%s\" is %d\n", containstring1, totalstring, strings.index(totalstring, containstring1))
	fmt.printf("\"%s\" first index in \"%s\" is %d\n", containstring2, totalstring, strings.index(totalstring, containstring2))
	fmt.printf("\"%s\" first index in \"%s\" is %d\n", containstring3, totalstring, strings.index(totalstring, containstring3))
	fmt.println("last index demo is:")
	fmt.printf("\"%s\" first index in \"%s\" is %d\n", containstring1, totalstring, strings.lastindex(totalstring, containstring1))
	fmt.printf("\"%s\" first index in \"%s\" is %d\n", containstring2, totalstring, strings.lastindex(totalstring, containstring2))
	fmt.printf("\"%s\" first index in \"%s\" is %d\n", containstring3, totalstring, strings.lastindex(totalstring, containstring3))
}

运行结果为:

first index demo is:
"go" first index in "hello go, i love cpp, i love cpp and go" is 6
"cpp" first index in "hello go, i love cpp, i love cpp and go" is 17
"java" first index in "hello go, i love cpp, i love cpp and go" is -1
last index demo is:
"go" first index in "hello go, i love cpp, i love cpp and go" is 37
"cpp" first index in "hello go, i love cpp, i love cpp and go" is 29
"java" first index in "hello go, i love cpp, i love cpp and go" is -1

如果需要查询非 ascii 编码的字符在父字符串中的位置,建议使用以下函数来对字符进行定位:

strings.indexrune(s string, r rune) int

字符串替换

replace() 用于将字符串 str 中的前 n 个字符串 old 替换为字符串 new,并返回一个新的字符串,如果 n = -1 则替换所有字符串 old 为字符串 new:

strings.replace(str, old, new string, n int) string

示例代码

func test4() {
	str1 := "hello hello hello hello hello hello"
	str2 := strings.replace(str1, "hello", "no", -1)
	fmt.println(str2)
}

这个函数的意思就是只要识别到有可以替换的字符串,并且对于最后一个数字嗯,并没有超过所限制的数量,那么就会将这个识别道德字符串替换为想要替换成的字符串,比如在这个例子当中当识别到字符串中含有哈喽,这个单词是就会将hello替换成no,前提是没有超过-1的限制,而因为-1的意思是,只要有字符串就进行替换,那么就会整个将这个字符串当中所有含有hello的字符串都替换为no

统计字符串出现次数

count() 用于计算字符串 str 在字符串 s 中出现的非重叠次数:

strings.count(s, str string) int

示例代码

func test5() {
	str1 := "hello world hello world hellhello world"
	fmt.println(strings.count(str1, "hello"))
	fmt.println(strings.count(str1, "world"))
}

重复字符串

repeat() 用于重复 count 次字符串 s 并返回一个新的字符串:

strings.repeat(s, count int) string

示例代码

func test6() {
	str := "hello go"
	fmt.println(strings.repeat(str, 10))
	fmt.println(strings.repeat(str, 2))
}

修改字符串大小写

tolower() 将字符串中的 unicode 字符全部转换为相应的小写字符:

strings.tolower(s) string

toupper() 将字符串中的 unicode 字符全部转换为相应的大写字符:

strings.toupper(s) string

示例代码

func test7() {
	str := "hello world this is test"
	fmt.println(strings.tolower(str))
	fmt.println(strings.toupper(str))
}

修剪字符串

你可以使用 strings.trimspace(s) 来剔除字符串开头和结尾的空白符号;如果你想要剔除指定字符,则可以使用 strings.trim(s, “cut”) 来将开头和结尾的 cut 去除掉。该函数的第二个参数可以包含任何字符,如果你只想剔除开头或者结尾的字符串,则可以使用 trimleft() 或者 trimright() 来实现。

示例代码

func test8() {
	str1 := "11hello world111"
	str2 := "  hello go    "
	fmt.println("去除空白")
	fmt.println(strings.trimspace(str1))
	fmt.println(strings.trimspace(str2))
	fmt.println("去除左侧空白")
	fmt.println(strings.trimleft(str2, " "))
	fmt.println("去除左侧字符1")
	fmt.println(strings.trimleft(str1, "1"))
	fmt.println("去除右侧字符1")
	fmt.println(strings.trimright(str1, "1"))
	fmt.println("去除左右两侧1")
	fmt.println(strings.trim(str1, "1"))
}

分割字符串

strings.fields(s) 将会利用 1 个或多个空白符号来作为动态长度的分隔符将字符串分割成若干小块,并返回一个 slice,如果字符串只包含空白符号,则返回一个长度为 0 的 slice。

strings.split(s, sep) 用于自定义分割符号来对指定字符串进行分割,同样返回 slice。

因为这 2 个函数都会返回 slice,所以习惯使用 for-range 循环来对其进行处理

示例代码

func test9() {
	str1 := "hello1 hello2 hello3      hello4"
	fmt.println("以一个空格为分隔符")
	s1 := strings.split(str1, " ")
	for _, t := range s1 {
		fmt.println(t)
	}
	fmt.println("以一个或多个空格为分隔符")
	s2 := strings.fields(str1)
	for _, t := range s2 {
		fmt.println(t)
	}
	str3 := "hello11hello22hello321321312hello4"
	fmt.println("以hello为分隔符")
	s3 := strings.split(str3, "hello")
	for _, t := range s3 {
		fmt.println(t)
	}
}

拼接 slice 到字符串

join() 用于将元素类型为 string 的 slice 使用分割符号来拼接组成一个字符串:

strings.join(sl []string, sep string) string

示例代码

func test10() {
	// 定义一个字符串切片
	sl := []string{"apple", "banana", "cherry"}
	// 使用空格作为分隔符拼接切片中的字符串
	result := strings.join(sl, " ")
	fmt.println(result) // 输出 "apple banana cherry"
	// 使用逗号和空格作为分隔符拼接切片中的字符串
	result2 := strings.join(sl, ", ")
	fmt.println(result2) // 输出 "apple, banana, cherry"
}

strconv

string类型的数据和其他类型的数据进行转换,实际上是借助这个包来完成的

这里只讲述最基本的内容,其他的内容之后再进行讲解

示例代码:

func test11() {
	str1 := "666"
	number, _ := strconv.atoi(str1)
	fmt.println(number)
	fmt.println(strconv.itoa(number + 5))
}

到此这篇关于go:strings包的基本使用的文章就介绍到这了,更多相关go strings包使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com