Go language strings package

Posted by lm_a_dope on Thu, 21 Nov 2019 23:03:45 +0100

Catalog

The strings package implements simple functions for manipulating characters.

func EqualFold

Determine whether the two utf-8 encoding strings (unicode uppercase, lowercase, and title characters are regarded as the same) are the same.

func EqualFold(s, t string) bool

Example:

func main() {

    str1 := "Golang"
    str2 := "golang"

    fmt.Println(strings.EqualFold(str1, str2)) //Output: true
}

HasPrefix

Determine whether s has prefix string prefix.

func HasPrefix(s, prefix string) bool

Example:

func main() {

    var str string = "Hello"

    fmt.Println(strings.HasPrefix(str, "He")) //true
    fmt.Println(strings.HasPrefix(str, "he")) //false
}

func HasSuffix

Determine whether s has suffix string suffix.

func HasSuffix(s, suffix string) bool

Example:

func main() {

    var str = "Hello"

    fmt.Println(strings.HasSuffix(str, "llo")) //true
    fmt.Println(strings.HasSuffix(str, "ll")) //false
}

func Contains

Determines whether the string s contains the substring substr.

func Contains(s, substr string) bool

Example:

func main() {

    var str = "China,Taiwan"

    fmt.Println(strings.Contains(str, "Taiwan")) //true
    fmt.Println(strings.Contains(str, "Japan")) //false
}

func ContainsRune

Determine whether the string s contains the utf-8 code value r.

func ContainsRune(s string, r rune) bool

Example:

func main() {

    var r rune = 'in'
    var str = "China"

    fmt.Println(strings.ContainsRune(str, r)) //true
    fmt.Println(strings.ContainsRune(str, 'day')) //false
}

func ContainsAny

Determines whether the string s contains any character in the string chars.

func ContainsAny(s, chars string) bool

Example:

func main() {

    var s = "I love you!,China"
    var chars = "I love you!"
    var test  = "day"

    fmt.Println(strings.ContainsAny(s, chars)) //true
    fmt.Println(strings.ContainsAny(s, test))  //false
}

func Count

Returns the number of times substr appears in string s. if substr is an empty string, then returns the unicode length + 1 of string s

func Count(s, substr string) int

Example:

func main() {

    var s    = "Jack,This is Dragon"
    var seq1 = "is"
    var seq2 = "Dragon"
    var seq3 = ""
    fmt.Println("Character string s The length is:", utf8.RuneCountInString(s))
    fmt.Println(strings.Count(s, seq1)) //2
    fmt.Println(strings.Count(s, seq2)) //1
    fmt.Println(strings.Count(s, seq3)) //15

}

func Index

The first occurrence of substring substr in string s. if it does not exist, return - 1.

func Index(s, substr string) int

Example:

func main() {

    var s       = "hello,world"
    var substr  = "o"
    var substr2 = "a"
    fmt.Println(strings.Index(s, substr)) //4
    fmt.Println(strings.Index(s, substr2)) //-1

}

func IndexByte

The position where character c first appears in s. if it does not exist, return - 1.

func IndexByte(s string, c byte) int

Example:

func main() {

    var s       = "hello,world"
    var c1 byte = 'o'
    var c2 byte = 'a'
    fmt.Println(strings.IndexByte(s, c1)) //4
    fmt.Println(strings.IndexByte(s, c2)) //-1

}

Topics: Go encoding