Golang (go language) standard Library analysis strings (2)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In fact, I do not understand what makes me stick down 1.1 points of analysis Pkg package, but the analysis of the time I feel very happy! Today we continue to strings bag

(1) func Index(s, sep string) int This function is to find the string, then return the current position, enter the string type, and then the position information of int
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Index ("Widuu", "I"))//1
Fmt. PRINTLN (Strings. Index ("Widuu", "U"))//3
}

[/php]

(2) func IndexAny(s, chars string) int This function is the same as the lookup, where the string first appears, and returns 1 if it does not exist.
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Indexany ("Widuu", "U"))//3
}

[/php]

(3) func IndexByte(s string, c byte) int , this function is to find the position of the first thick line, but this time C is a byte type, find the return position, can not find return-1
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Indexbyte ("Hello Xiaowei", ' X '))//6
}

[/php]

(4) func IndexRune(s string, r rune) int , or find the location, but this time is the rune type of
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Indexrune ("Widuu", Rune (' W ')))//0
}

[/php]

(5) func IndexFunc(s string, f func(rune) bool) int This function everyone knows, is through the type of conversion to use the function to find the location, we come to the code to see HA
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Indexfunc ("Nihaoma", split))//3
}

Func split (R rune) bool {
if r = = ' A ' {
return True
}
return False
}

[/php]

(6) func Join(a []string, sep string) string , this is similar to PHP implode, this function is a []string slice through the delimiter, split into a string
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
s: = []string{"Hello", "word", "Xiaowei"}
Fmt. PRINTLN (Strings. Join (S, "-"))//hello-word-xiaowei
}

[/php]

(7) func LastIndex(s, sep string) int See this everyone may also understand the search is the last occurrence of the position, just opposite the index
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. LastIndex ("Widuu", "U"))//4
}

[/php]

(8) func LastIndexAny(s, chars string) int This is the opposite of Indexany, and the last one to find
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Lastindexany ("Widuu", "U"))//4
}

[/php]

(9) func LastIndexFunc(s string, f func(rune) bool) int , this is the opposite of indexfunc, through the function to find the last position
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Lastindexfunc ("Nihaoma", split))//6
}

Func split (R rune) bool {
if r = = ' A ' {
return True
}
return False
}

[/php]

(x) func Map(mapping func(rune) rune, s string) string , the function reads the characters in S, passes in the mapping function, and then returns the word linked, which is plainly the string of each character through the processing of the mapping function, and finally returns the processed string, if the processing is not correct, then discard the character
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Map (func (R rune) Rune {
if r = = ' A ' {
Return ' I '
}
Return r
}, "Hello Waduu"))//hello Widuu
}

[/php]

(one) func Repeat(s string, count int) string , the function is to output several repeating strings, and count represents the number of repetitions
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Repeat ("Widuu", 3))//widuuwiduuwiduu
}

[/php]

The func Replace(s, old, new string, n int) string function is to replace a character in a string with a character you define, n is the value to replace, if N<0 is the replacement of all, the knowledge returned is a copy [Php]import ("FMT" "strings") func main () {s: = " Widuuweb "FMT. PRINTLN (Strings. Replace (S, "U", "a", 1))//widauwebfmt. PRINTLN (Strings. Replace (S, "U", "a",-1))//widaawebfmt. Println (s)//widuuweb}[/php] (), there is a join there is func Split(s, sep string) []string split this is the string according to the specified delimiter cut into slice
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Split ("A,b,c,d,e", ","))//[a b c D E]
}

[/php]

() func SplitAfter(s, sep string) []string , this function is preceded by the completion of the cut before the addition of the SEP separator
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Splitafter ("A,b,c,d", ","))//[a, B, C, d]
}

[/php]

(+) func SplitAfterN(s, sep string, n int) []string The function S is divided according to Sep, returning the slice of the sub-string after the split, and split, just the returned substring retains sep, if Sep is empty, then each character is split
[PHP]
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"]
}

[/php]

func SplitN(s, sep string, n int) []stringThis is the time to cut the string and define its length, and if Sep is empty, then each character is split
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. SPLITN ("A,b,c", ",", 2))//[a B,c]
}
[/php]

Every day only a little Golang standard library, convenient for everyone to learn and use, more time to understand the standard library, we do more hands, if you like please continue to follow us

Golang Standard Library

Without permission, may not reprint this station any article: the Micro Degree Network»golang Explanation (go language) standard Library analysis strings (2)

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.