A summary of the method of string cutting in go language programming

Source: Internet
Author: User

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

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.