在Go语言中,字符串不同于Java,C ++,Python等其他语言。它是一系列宽度可变的字符,其中每个字符都使用UTF-8编码由一个或多个字节表示。在Go字符串中,可以使用以下函数将字符串拆分为一个切片。这些函数是在字符串包下定义的,因此,您必须在程序中导入字符串包才能访问这些函数:
1.Split:此函数将字符串拆分为由给定分隔符分隔的所有子字符串,并返回包含这些子字符串的切片。
语法:
func Split(str, sep string) []string
在这里,str是字符串,sep是分隔符。 如果str不包含给定的sep且sep为非空,则它将返回长度为1的切片,其中仅包含str。 或者,如果sep为空,则它将在每个UTF-8序列之后拆分。 或者,如果str和sep均为空,则它将返回一个空切片。
package main
import (
    "fmt"
    "strings"
)
func main() {
    //创建和初始化字符串
    str1 := "Welcome, to the, online portal, of nhooo"
    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("", "nhooo, geeks")
    // 显示结果
    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 nhooo String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome to the online portal of nhooo] 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: []
2. SplitAfter:此函数在给定分隔符的每个实例之后将字符串拆分为所有子字符串,并返回包含这些子字符串的切片。
语法:
func SplitAfter(str, sep string) []string
在这里,str是字符串,sep是分隔符。 如果str不包含给定的sep且sep为非空,则它将返回长度为1的切片,其中仅包含str。 或者,如果sep为空,则它将在每个UTF-8序列之后拆分。 或者,如果str和sep均为空,则它将返回一个空切片。
package main
import (
    "fmt"
    "strings"
)
func main() {
    //创建和初始化字符串
    str1 := "Welcome, to the, online portal, of nhooo"
    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("", "nhooo, geeks")
    //显示结果
    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 nhooo String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome, to the, online portal, of nhooo] 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]
3. 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"
) 
  
func main() { 
  
    //创建和初始化字符串
    str1 := "Welcome, to the, online portal, of nhooo"
    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("", "nhooo, geeks", 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 nhooo String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: [Welcome, to the, online portal, of nhooo] Result 2: [M y dog name is Dollar] Result 3: [I like to play Ludo] Result 4: []