📜  如何在 Golang 中拆分字符串?

📅  最后修改于: 2021-10-25 02:18:12             🧑  作者: Mango

在 Go 语言中,字符串不同于Java、C++、 Python等其他语言。它是一系列可变宽度字符,其中每个字符都由一个或多个使用 UTF-8 编码的字节表示。在 Go 字符串,您可以借助以下函数将字符串拆分为切片。这些函数是在字符串包下定义的,因此您必须在程序中导入字符串包才能访问这些函数:

1. 拆分:此函数将字符串拆分为由给定分隔符分隔的所有子字符串,并返回包含这些子字符串的切片。

句法:

func Split(str, sep string) []string

这里, str是字符串,sep 是分隔符。如果str不包含给定的sep并且sep非空,那么它将返回一个长度为 1 的切片,其中只包含str 。或者如果sep为空,那么它将在每个 UTF-8 序列之后拆分。或者如果strsep都为空,那么它将返回一个空切片。

例子:

// Go program to illustrate how to split a string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing the strings
    str1 := "Welcome, to the, online portal, of GeeksforGeeks"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"
  
    // Displaying strings
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)
  
    // Splitting the given strings
    // Using Split() function
    res1 := strings.Split(str1, ",")
    res2 := strings.Split(str2, "")
    res3 := strings.Split(str3, "!")
    res4 := strings.Split("", "GeeksforGeeks, geeks")
  
    // Displaying the result
  
    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 GeeksforGeeks
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome  to the  online portal  of GeeksforGeeks]
Result 2:  [M y   d o g   n a m e   i s   N a w a b]
Result 3:  [I like to play Ludo]
Result 4:  []

2. SplitAfter:此函数在给定分隔符的每个实例之后将字符串拆分为所有子字符串,并返回包含这些子字符串的切片。

句法:

func SplitAfter(str, sep string) []string

这里, str是字符串,sep 是分隔符。如果str不包含给定的sep并且sep非空,那么它将返回一个长度为 1 的切片,其中只包含str 。或者如果sep为空,那么它将在每个 UTF-8 序列之后拆分。或者如果strsep都为空,那么它将返回一个空切片。

例子:

// Go program to illustrate how to split a string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing the strings
    str1 := "Welcome, to the, online portal, of GeeksforGeeks"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"
  
    // Displaying strings
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)
  
    // Splitting the given strings
    // Using SplitAfter() function
    res1 := strings.SplitAfter(str1, ",")
    res2 := strings.SplitAfter(str2, "")
    res3 := strings.SplitAfter(str3, "!")
    res4 := strings.SplitAfter("", "GeeksforGeeks, geeks")
  
    // Displaying the result
    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 GeeksforGeeks
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome,  to the,  online portal,  of GeeksforGeeks]
Result 2:  [M y   d o g   n a m e   i s   N a w a b]
Result 3:  [I like to play Ludo]
Result 4:  []

3. SplitAfterN:此函数在给定分隔符的每个实例之后将字符串拆分为所有子字符串,并返回包含这些子字符串的切片。

句法:

func SplitAfterN(str, sep string, m int) []string

这里, str是字符串, sep是分隔符, m 用于查找要返回的子字符串的数量。在这里,如果m>0 ,那么它最多返回m个子串并且最后一个字符串子串不会分裂。如果m == 0 ,那么它将返回 nil。如果m<0 ,则它将返回所有子字符串。

例子:

// Go program to illustrate how to split a string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing the strings
    str1 := "Welcome, to the, online portal, of GeeksforGeeks"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"
  
    // Displaying strings
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)
  
    // Splitting the given strings
    // Using SplitAfterN() function
    res1 := strings.SplitAfterN(str1, ",", 2)
    res2 := strings.SplitAfterN(str2, "", 4)
    res3 := strings.SplitAfterN(str3, "!", 1)
    res4 := strings.SplitAfterN("", "GeeksforGeeks, geeks", 3)
  
    // Displaying the result
    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 GeeksforGeeks
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome,  to the, online portal, of GeeksforGeeks]
Result 2:  [M y   dog name is Dollar]
Result 3:  [I like to play Ludo]
Result 4:  []