Golang 如何分割一个字符串
在Go语言中,字符串与其他语言如Java、C++、Python等不同。它是一个宽度可变的字符序列,每一个字符都由一个或多个使用UTF-8编码的字节表示。在Go字符串中,你可以在以下函数的帮助下将一个字符串分割成一个片断。这些函数是在strings包中定义的,所以你必须在你的程序中导入strings包来访问这些函数:
1.Split: 这个函数将一个字符串分割成由给定分隔符分隔的所有子字符串,并返回包含这些子字符串的片断。
语法
func Split(str, sep string) []string
这里, str是字符串,sep是分隔符。如果str不包含给定的sep,并且sep 不是空的,那么它将返回一个长度为1的片断,其中只包含str。或者如果sep 是空的,那么它将在每个UTF-8序列之后进行分割。或者如果str和sep 都是空的,那么它将返回一个空的片断。
例子
// 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 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 都是空的,那么它将返回一个空的片断。
示例
// 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: []