如何在Golang中分割字符串()

在Go语言中, 弦与其他语言(例如Java, C ++, python等等。它是一系列可变宽度字符, 其中每个字符都使用UTF-8编码由一个或多个字节表示。在Go字符串中, 可以使用以下功能将字符串拆分为一个切片。这些函数是在字符串包下定义的, 因此, 你必须在程序中导入字符串包才能访问这些功能:
1.拆分:此函数将字符串拆分为由给定分隔符分隔的所有子字符串, 并返回包含这些子字符串的切片。
语法如下:

func Split(str, sep string) []string

这里, str是字符串, sep是分隔符。如果str不包含给定九月和九月为非空, 则它将返回长度为1的切片, 其中仅包含str。或者, 如果九月为空, 则它将在每个UTF-8序列之后拆分。或者如果两者都str和九月为空, 则将返回一个空切片。
例子:
//Go program to illustrate how to split a string package mainimport ( "fmt" "strings" )//Main function func main() {//Creating and initializing the strings str1 := "Welcome, to the, online portal, of srcmini" 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( "" , "srcmini, geeks" )//Displaying the resultfmt.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 srcminiString 2:My dog name is DollarString 3:I like to play LudoResult 1:[Welcometo theonline portalof srcmini]Result 2:[M yd o gn a m ei sN a w a b]Result 3:[I like to play Ludo]Result 4:[]

2. SplitAfter:此函数在给定分隔符的每个实例之后将字符串拆分为所有子字符串, 并返回包含这些子字符串的切片。
语法如下:
func SplitAfter(str, sep string) []string

这里, str是字符串, sep是分隔符。如果str不包含给定九月和九月为非空, 则它将返回长度为1的切片, 其中仅包含str。或者, 如果九月为空, 则它将在每个UTF-8序列之后拆分。或者如果两者都str和九月为空, 则将返回一个空切片。
例子:
//Go program to illustrate how to split a string package mainimport ( "fmt" "strings" )//Main function func main() {//Creating and initializing the strings str1 := "Welcome, to the, online portal, of srcmini" 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( "" , "srcmini, 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 srcminiString 2:My dog name is DollarString 3:I like to play LudoResult 1:[Welcome, to the, online portal, of srcmini]Result 2:[M yd o gn a m ei sN a w a b]Result 3:[I like to play Ludo]Result 4:[]

3. SplitAfterN:此函数在给定分隔符的每个实例之后将字符串拆分为所有子字符串, 并返回包含这些子字符串的切片。
语法如下:
func SplitAfterN(str, sep string, m int) []string

这里, str是字符串, 九月是分隔符, m用于查找要返回的子字符串的数量。在这里, 如果m> 0, 那么最多返回米子字符串和最后一个字符串子字符串将不会拆分。如果m == 0, 则它将返回nil。如果m < 0, 那么它将返回所有子字符串。
例子:
//Go program to illustrate how to split a string package mainimport ( "fmt" "strings" )//Main function func main() {//Creating and initializing the strings str1 := "Welcome, to the, online portal, of srcmini" 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( "" , "srcmini, 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)}

【如何在Golang中分割字符串()】输出如下:
String 1:Welcome, to the, online portal, of srcminiString 2:My dog name is DollarString 3:I like to play LudoResult 1:[Welcome, to the, online portal, of srcmini]Result 2:[M ydog name is Dollar]Result 3:[I like to play Ludo]Result 4:[]

    推荐阅读