如何在 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: []
相关文章
在 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 语法,并提供一些代码中的实际使用示例。
在 Python 中是否存在 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)。