当前位置: 首页 > article >正文

Go:strings包的基本使用

文章目录

  • string
    • 前缀和后缀
    • 字符串包含
    • 判断子字符串或字符在父字符串中出现的位置
    • 字符串替换
    • 统计字符串出现次数
    • 重复字符串
    • 修改字符串大小写
    • 修剪字符串
    • 分割字符串
    • 拼接 slice 到字符串
  • strconv

本篇主要总结的是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))
}

http://www.kler.cn/news/362322.html

相关文章:

  • MySQL中的最左前缀匹配原则
  • ​​Spring6梳理17——基于XML的自动装配
  • 输入输出--I/O流【C++提升】
  • MATLAB代码优化
  • 学习TOGAF架构
  • Java Swing的优秀开源项目学习推荐(UI、数据结构与设计模式)
  • Qt学习笔记第21到30讲
  • 『 Linux 』HTTP(三)
  • 【火山引擎】语音合成 | HTTP接口 | 一次性合成 | python
  • AnaTraf | 网络流量分析仪:网络故障排除的利器
  • 038_基于php校园Flea Market售货平台
  • C/C++:指针数组与数组指针
  • C++:模板(2)
  • AI+云原生时代,高校该如何创新教育模式
  • git取消被跟踪的文件并忽略
  • 分享一位教授关于VLAN标签在交换机中的转发原理的教学
  • 线性可分支持向量机的原理推导 线性分隔超平面关于任意样本点 (x_i,y_i)的几何间隔 公式解析
  • 数据结构与算法:高级数据结构与实际应用
  • <el-input-number> 回车自动失去焦点
  • 如何在Python网络爬虫中处理动态网页?
  • rootless模式下istio ambient鉴权策略
  • Oracle分区表改造(二):通过在线重定义改造为分区表
  • 几何算法系列:空间实体体积计算公式推导
  • Rust : FnOnce、线程池与多策略执行
  • 11.useComponentDidMount
  • neo4j 中日期时间 时间戳处理