如何在 golang 中拆分字符串?-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > >

如何在 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 语法,并提供一些代码中的实际使用示例。

发布时间: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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图