1.func fields (s string) []string, this function is to divide the string by 1:n a space, and the last return is
[]string's slices
Copy CodeThe code is 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 See, this is based on a custom function split
Copy CodeThe code is as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. Fieldsfunc ("Widuunhellonword", Split)//[Widuu Hello Word] split by n character
}
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 divide a []string slice into a string by a delimiter.
Copy CodeThe code is 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's a join there's split this is to cut the string into slice according to the specified delimiter.
Copy CodeThe code is 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 in front of the cut after the completion of the addition of the SEP separator
Copy CodeThe code is 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 splits according to Sep, returns the slice of the sub-string after the split, and split, just returns the substring reserved sep, if Sep is empty, So every single character is split.
Copy CodeThe code is 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, this is the time when the string is cut to define its length, and if Sep is empty, then each character is split
Copy CodeThe code is as follows:
Import (
"FMT"
"Strings"
)
Func Main () {
Fmt. PRINTLN (Strings. SPLITN ("A,b,c", ",", 2))//[a B,c]
}
A summary of the method of string cutting in go language programming