迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Go >

如何在 Golang 中拆分字符串?

作者:迹忆客 最近更新:2023/01/08 浏览次数:

在 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 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

C# 中的 goto 语句

发布时间:2024/02/02 浏览次数:184 分类:编程语言

本教程演示了如何在 C# 中使用 goto 以及何时使用它会有所帮助本教程将演示如何在 C# 中使用 goto 语法,并提供一些代码中的实际使用示例。

在 Python 中是否存在 goto 语句

发布时间:2023/12/20 浏览次数:197 分类:Python

本文为你提供了 Python 中是否存在 goto 语句的答案。本文为你提供了 Python 中是否存在 goto 语句的答案。基本上,Python 不支持 goto 语句。

Java Goto

发布时间:2023/08/07 浏览次数:83 分类:Java

与其他编程语言不同,Java 没有 goto。 相反,Java 包含关键字 label。关键字 label 的作用是改变程序的流程,根据指定的条件跳转到程序的另一段。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便