A summary of string-cutting methods in go language programming _golang

Source: Internet
Author: User

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]
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.