如何在 golang 中拆分字符串?
在 go 语言中,字符串不同于 java、c 、python 等其他语言。它是一个可变宽度字符序列,其中每个字符都使用 utf-8 编码由一个或多个字节表示。 在 go 字符串中,可以借助以下函数将字符串拆分为切片。 这些函数是在 strings 包下定义的,因此我们必须在程序中导入 strings 包才能访问这些函数:
split
此函数将字符串拆分为由给定分隔符分隔的所有子字符串,并返回包含这些子字符串的切片。
语法:
func split(str, sep string) []string
这里,str 是字符串,sep 是分隔符。 如果 str 不包含给定的 sep 并且 sep 非空,则它将返回一个长度为 1 的切片,其中仅包含 str。 或者,如果 sep 为空,则它将在每个 utf-8 序列之后拆分。 或者如果 str 和 sep 都为空,那么它将返回一个空切片。
下面我们看一个示例
package main
import (
"fmt"
"strings"
)
// main 函数
func main() {
// 初始化字符串
str1 := "welcome, to the, online portal, of jiyik.com"
str2 := "my dog name is dollar"
str3 := "i like to play ludo"
// 打印字符串
fmt.println("string 1: ", str1)
fmt.println("string 2: ", str2)
fmt.println("string 3: ", str3)
// 拆分给定字符串
// 使用 split() 方法
res1 := strings.split(str1, ",")
res2 := strings.split(str2, "")
res3 := strings.split(str3, "!")
res4 := strings.split("", "jiyik.com, jiyik")
// 显示结果
fmt.println("\nresult 1: ", res1)
fmt.println("result 2: ", res2)
fmt.println("result 3: ", res3)
fmt.println("result 4: ", res4)
}
上面示例的输出结果如下
string 1: welcome, to the, online portal, of jiyik.com
string 2: my dog name is dollar
string 3: i like to play ludo
result 1: [welcome to the online portal of jiyik.com]
result 2: [m y d o g n a m e i s d o l l a r]
result 3: [i like to play ludo]
result 4: []
splitafter
此函数在给定分隔符的每个实例之后将字符串拆分为所有子字符串,并返回包含这些子字符串的切片。
语法
func splitafter(str, sep string) []string
这里,str 是字符串,sep 是分隔符。 如果 str 不包含给定的 sep 并且 sep 非空,则它将返回一个长度为 1 的切片,其中仅包含 str。 或者,如果 sep 为空,则它将在每个 utf-8 序列之后拆分。 或者如果 str 和 sep 都为空,那么它将返回一个空切片。
下面我们看一个示例
package main
import (
"fmt"
"strings"
)
// main 函数
func main() {
// 初始化字符串
str1 := "welcome, to the, online portal, of jiyik.com"
str2 := "my dog name is dollar"
str3 := "i like to play ludo"
// 显示字符串
fmt.println("string 1: ", str1)
fmt.println("string 2: ", str2)
fmt.println("string 3: ", str3)
// 拆分给定的字符串
// 使用 splitafter() 函数
res1 := strings.splitafter(str1, ",")
res2 := strings.splitafter(str2, "")
res3 := strings.splitafter(str3, "!")
res4 := strings.splitafter("", "jiyik.com, jiyik")
// 显示结果
fmt.println("\nresult 1: ", res1)
fmt.println("result 2: ", res2)
fmt.println("result 3: ", res3)
fmt.println("result 4: ", res4)
}
上述示例运行结果如下
string 1: welcome, to the, online portal, of jiyik.com
string 2: my dog name is dollar
string 3: i like to play ludo
result 1: [welcome, to the, online portal, of jiyik.com]
result 2: [m y d o g n a m e i s d o l l a r]
result 3: [i like to play ludo]
result 4: []
splitaftern
此函数在给定分隔符的每个实例之后将字符串拆分为所有子字符串,并返回包含这些子字符串的切片。
语法
func splitaftern(str, sep string, m int) []string
这里,str 是字符串,sep 是分隔符,m 用于查找要返回的子字符串数。 这里,如果 m>0,则最多返回m个子串,最后一个子串不会分裂。 如果 m == 0 ,那么它将返回 nil。 如果 m<0,则返回所有子串。
下面我们看示例
package main
import (
"fmt"
"strings"
)
// main 函数
func main() {
// 初始化字符串
str1 := "welcome, to the, online portal, of jiyik.com"
str2 := "my dog name is dollar"
str3 := "i like to play ludo"
// 显示字符串
fmt.println("string 1: ", str1)
fmt.println("string 2: ", str2)
fmt.println("string 3: ", str3)
// 使用 splitaftern() 方法拆分给定的字符串
res1 := strings.splitaftern(str1, ",", 2)
res2 := strings.splitaftern(str2, "", 4)
res3 := strings.splitaftern(str3, "!", 1)
res4 := strings.splitaftern("", "jiyik.com, jiyik", 3)
// 显示结果
fmt.println("\nresult 1: ", res1)
fmt.println("result 2: ", res2)
fmt.println("result 3: ", res3)
fmt.println("result 4: ", res4)
}
上面示例输出结果如下所示
string 1: welcome, to the, online portal, of jiyik.com
string 2: my dog name is dollar
string 3: i like to play ludo
result 1: [welcome, to the, online portal, of jiyik.com]
result 2: [m y dog name is dollar]
result 3: [i like to play ludo]
result 4: []
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 python pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 javascript 中验证 google recaptcha 第 2 版
发布时间:2024/03/23 浏览次数:193 分类:javascript
-
本文演示了如何在 javascript 中验证 google recaptcha。
c# 中的 goto 语句
发布时间:2024/02/02 浏览次数:184 分类:编程语言
-
本教程演示了如何在 c# 中使用 goto 以及何时使用它会有所帮助本教程将演示如何在 c# 中使用 goto 语法,并提供一些代码中的实际使用示例。
发布时间:2023/12/20 浏览次数:197 分类:python
-
本文为你提供了 python 中是否存在 goto 语句的答案。本文为你提供了 python 中是否存在 goto 语句的答案。基本上,python 不支持 goto 语句。
避免 python中的 typeerror: input expected at most 1 argument, got 3 错误
发布时间:2023/07/08 浏览次数:671 分类:python
-
python 中避免 typeerror: input expected atmost 1 argument, got 3 error在python编程中,我们有两个内置方法来获取用户的输入:input(prompt)和 raw_input(prompt)。