1.func Fields (s string) []string, the function of which is to split the string according to the 1:n space. The last return is
[]string slices
Copy Code code as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. Fields ("Hello Widuu golang")//out [Hello Widuu Golang]
}
2.func Fieldsfunc (S string, F func (rune) bool) []string A look at it, this is based on the custom function split
Copy Code code as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. Fieldsfunc ("Widuunhellonword", split))//[Widuu Hello Word] based on n character segmentation
}
Func split (S rune) bool {
if s = = ' n ' {
return True
}
return False
}
3.func Join (A []string, Sep String) string, which is similar to the implode in PHP, this function is to split a []string slice into a string
Copy Code code as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
s: = []string{"Hello", "word", "Xiaowei"}
Fmt. PRINTLN (Strings. Join (S, "-"))//Hello-word-xiaowei
}
4.func Split (S, Sep string) []string, there is a join there is a split this is the string by the specified delimiter cut to slice
Copy Code code as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. Split ("A,b,c,d,e", ","))//[a b c D E]
}
5.func Splitafter (S, Sep string) []string, this function is preceded by the cut is completed and then followed by the SEP separator
Copy Code code as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. Splitafter ("A,b,c,d", ","))//[a, B, C, d]
}
6.func Splitaftern (S, Sep string, n int) []string the function S, according to the SEP partition, returns the slice of the substring after the split, except that the returned substring retains sep, if the Sep is empty, So every single character is split
Copy Code code as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. Splitaftern ("A,b,c,d,r", ",", 4))//["A," "B," "C," "D,r"]
Fmt. PRINTLN (Strings. Splitaftern ("A,b,c,d,r", ",", 5))//["A," "B," "C," "D," "R"]
}
7.func splitn (S, Sep string, n int) []string, which defines the length of the string when it is cut, and if Sep is empty, then each character is split
Copy Code code as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. SPLITN ("A,b,c", ",", 2))//[a B,c]
}