Golang字符串和strconv包实践|青训营笔记

879 阅读2分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的的第3篇笔记

image.png

1.1 前缀和后缀

HasFrefix判断字符串s是否以prefix开头

strings.HasPrefix(s, prefix string) bool

HasSuffix判断字符串s是否以suffix结尾

strings.HasSuffix(s, suffix string) bool
package main

import (
    "fmt"
    "strings"
)

func main() {
    var str string = "This is an example of a string"
    fmt.Printf("T/F? Does the string "%s" have prefix %s? ", str, "Th")
    fmt.Printf("%t\n", strings.HasPrefix(str, "Th"))
}

输出:

T/F? Does the string "This is an example of a string" have prefix Th? true

这个例子同样演示了转义字符\和格式化字符串的使用。

1.2 字符串包含关系

Contains判断字符串s是否包含substr

strings.Contains(s, substr string) bool

1.6.3 判断子字符串或字符在父字符串中出现的位置(索引)

\

Index返回字符串 str在字符串 s中的索引(str的第一个字符的索引),-1 表示字符串s不包含字符串str:

strings.Index(s, str string) int

\

LastIndex返回字符串str在字符串 s中最后出现位置的索引(str的第一个字符的索引),-1 表示字符串s不包含字符串 str:

strings.LastIndex(s, str string) int

如果ch是非 ASCII 编码的字符,建议使用以下函数来对字符进行定位:

strings.IndexRune(s string, r rune) int
package main

import (
    "fmt"
    "strings"
)

func main() {
    var str string = "Hi, I'm Marc, Hi."

    fmt.Printf("The position of "Marc" is: ")
    fmt.Printf("%d\n", strings.Index(str, "Marc"))

    fmt.Printf("The position of the first instance of "Hi" is: ")
    fmt.Printf("%d\n", strings.Index(str, "Hi"))
    fmt.Printf("The position of the last instance of "Hi" is: ")
    fmt.Printf("%d\n", strings.LastIndex(str, "Hi"))

    fmt.Printf("The position of "Burger" is: ")
    fmt.Printf("%d\n", strings.Index(str, "Burger"))
}

输出:

The position of "Marc" is: 8
The position of the first instance of "Hi" is: 0
The position of the last instance of "Hi" is: 14
The position of "Burger" is: -1

1.4 字符串替换

\

Replace用于将字符串 str中的前n个字符串old替换为字符串new,并返回一个新的字符串,如果n=-1则替换所有字符串old为字符串new:

strings.Replace(str, old, new, n) string

1.5 统计字符串出现次数

\

Count用于计算字符串str在字符串s中出现的非重叠次数:

strings.Count(s, str string) int
package main

import (
    "fmt"
    "strings"
)

func main() {
    var str string = "Hello, how is it going, Hugo?"
    var manyG = "gggggggggg"

    fmt.Printf("Number of H's in %s is: ", str)
    fmt.Printf("%d\n", strings.Count(str, "H"))

    fmt.Printf("Number of double g's in %s is: ", manyG)
    fmt.Printf("%d\n", strings.Count(manyG, "gg"))
}

输出:

Number of H's in Hello, how is it going, Hugo? is: 2
Number of double g’s in gggggggggg is: 5
func main() {
   a := "hello"
   fmt.Println(strings.Contains(a, "ll"))                // true
   fmt.Println(strings.Count(a, "l"))                    // 2
   fmt.Println(strings.HasPrefix(a, "he"))               // true
   fmt.Println(strings.HasSuffix(a, "llo"))              // true
   fmt.Println(strings.Index(a, "ll"))                   // 2
   fmt.Println(strings.Join([]string{"he", "llo"}, "-")) // he-llo
   fmt.Println(strings.Repeat(a, 2))                     // hellohello
   fmt.Println(strings.Replace(a, "e", "E", -1))         // hEllo
   fmt.Println(strings.Split("a-b-c", "-"))              // [a b c]
   fmt.Println(strings.ToLower(a))                       // hello
   fmt.Println(strings.ToUpper(a))                       // HELLO
   fmt.Println(len(a))                                   // 5
   b := "你好"
   fmt.Println(len(b)) // 6
}