Golang Tutorial (Go language) standard library analysis starts with the string

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

Today we continue to talk about the Golang standard Library of string This package, I chose this is to see the next package really needs this feature so we come to this package, the string package implements a simple function method to manipulate the string. The type of reader is simple because it returns a *reader this uses the IO we talked about, and it doesn't say much about getting into the chase.

(1) func Contains(s, substr string) bool This function is to find out if a character exists in the string, and there is a return true
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Contains ("Widuu", "wi"))//true
Fmt. PRINTLN (Strings. Contains ("wi", "Widuu"))//false
}
[/php]

(2) func ContainsAny(s, chars string) bool This is whether the query string contains more than one character
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Containsany ("Widuu", "W&d"))//true
}
[/php]

(3) func ContainsRune(s string, r rune) bool , where the side is of course the string contains the rune type, where the rune type is UTF8. Runecountstring can fully represent the type of all Unicode characters
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Containsrune ("Widuu", Rune (' W ')))//true
Fmt. PRINTLN (Strings. Containsrune ("Widuu",))//fasle
}
[/php]

(4) func Count(s, sep string) int The function of this is to output the number of characters that match in a string
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Count ("Widuu", "UU"))//1
Fmt. PRINTLN (Strings. Count ("Widuu", "U"))//2
}
[/php]

(5) func EqualFold(s, t string) bool This is to determine if the s,t two strings are equal UFF8 encoded in the case of a full lowercase
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Equalfold ("Widuu", "Widuu"))//true
}
[/php]

(6) func Fields(s string) []string , the function is to divide the string according to 1:n space, and the last return is []string slice
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Fields ("Hello Widuu Golang"))//out [Hello Widuu Golang]
}
[/php]

(7) func FieldsFunc(s string, f func(rune) bool) []string See, this is based on a custom function split
[PHP]
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
}
[/php]
(8) To func HasPrefix(s, prefix string) bool determine whether a string starts with
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Hasprefix ("Widuu_web", "Widuu"))//true
}
[/php]

(9) func HasSuffix(s, suffix string) bool , determine if there is any suffix, return bool value
[PHP]
Import (
"FMT"
"Strings"
)

Func Main () {
Fmt. PRINTLN (Strings. Hassuffix ("Nihaowiduu", "Widuu"))//true
}
[/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, do not reprint this site any article: Micro network»golang explanation (go language) standard library analysis of the string begins

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.