Go基础_1_27 字符串常用方法

Tutorial: Go初级 Category: Go Published: 2026-04-07 13:58:26 Views: 21 Likes: 0 Comments: 0
package main

import (
    "fmt"
    "strings"
)

func main() {
    /*
        strings包下的关于字符串的函数

    */

    s1 := "helloworld"
    // 1.是否包含指定的内容-->bool
    fmt.Println(strings.Contains(s1, "abc")) // false
    // 2.是否包含chars中任意的一个字符即可
    fmt.Println(strings.ContainsAny(s1, "abcd")) // true
    // 3.统计substr在s中出现的次数
    fmt.Println(strings.Count(s1, "lloo")) // 0

    // 4.以xxx前缀开头,以xxx后缀结尾
    s2 := "20190525课堂笔记.txt"
    if strings.HasPrefix(s2, "201905") {
        fmt.Println("19年5月的文件。。")
    }
    if strings.HasSuffix(s2, ".txt") {
        fmt.Println("文本文档。。")
    }

    // 索引
    // helloworld
    fmt.Println(strings.Index(s1, "lloo"))     // 查找substr在s中的位置,如果不存在就返回-1    -1
    fmt.Println(strings.IndexAny(s1, "abcdh")) // 查找chars中任意的一个字符,出现在s中的位置    0
    fmt.Println(strings.LastIndex(s1, "l"))    // 查找substr在s中最后一次出现的位置            8

    // 字符串的拼接
    ss1 := []string{"abc", "world", "hello", "ruby"}
    s3 := strings.Join(ss1, "-")
    fmt.Println(s3) // abc-world-hello-ruby

    // 切割
    s4 := "123,4563,aaa,49595,45"
    ss2 := strings.Split(s4, ",")
    fmt.Println(ss2) // [123 4563 aaa 49595 45]
    for i := 0; i < len(ss2); i++ {
        fmt.Println(ss2[i])
    }

    // 重复,自己拼接自己count次
    s5 := strings.Repeat("hello", 5)
    fmt.Println(s5) // hellohellohellohellohello

    // 替换
    // helloworld
    s6 := strings.Replace(s1, "l", "*", -1)
    fmt.Println(s6) // he**owor*d

    s7 := "heLLo WOrlD**123.."
    fmt.Println(strings.ToLower(s7)) // hello world**123..
    fmt.Println(strings.ToUpper(s7)) // HELLO WORLD**123..

    /*
        截取子串:
        substring(start,end)-->substr
        str[start:end]-->substr
            包含start,不包含end下标
    */

    fmt.Println(s1)     // helloworld
    s8 := s1[:5]        //
    fmt.Println(s8)     // hello
    fmt.Println(s1[5:]) // world
}