golang之string标准库(一)

1:Contains(s,substr string) bool:字符串s是否包含substr,包含返回true,不包含返回false

    flag := strings.Contains("hello world", "wor")
    if flag {
        fmt.Println("str is contain substr")
    } else {
        fmt.Println("str is not contain substr")
    }

2:ContainAny(s ,chars string) bool:字符串s是否包含Unicode chars中任意字符,包含返回true,不包含返回false

    flag = strings.ContainsAny("hello world", "h&w&o")
    if flag {
        fmt.Println("str is contain any chars")
    } else {
        fmt.Println("str is not contain chars")
    }

3:ContainsRune(s string,r rune)bool :判断字符串s是否包含ASCII字符r,包含返回true,不包含返回false

    flag = strings.ContainsRune("hello world", 'd')
    if flag {
        fmt.Println("containsRune exist")
    } else {
        fmt.Println("containrune not exist")
    }

4:LastIndex(s,substr string) int:返回子串substr在父串s最后一次出现的位置,存在返回下标,不存在返回-1

    index := strings.LastIndex("hello world", "o")
    if index != -1 {
        fmt.Println("LastIndex exist", index)
    } else {
        fmt.Println("LastIndex not exist")
    }

5:IndexRune(s string,r sune)int:返回ASCII字符r,在父串中第一次出现的位置,成功返回索引,失败返回-1

    index = strings.IndexRune("hello world", 'o')
    if index != -1 {
        fmt.Println("IndexRune exist", index)
    } else {
        fmt.Println("IndexRune not exist")
    }

6:IndexAny(s,chars string)int:返回chars中任意字符出现在父串s的第一次出现的位置,成功返回索引,失败,返回-1

    index = strings.IndexAny("hello world", "h&w&o")
    if index != -1 {
        fmt.Println("IndexAny exist :", index)
    } else {
        fmt.Println("Index not exist")
    }

7:LastIndexAny(s ,chars string) int:返回chars中任意字符出现在父串s的最后一次出现的位置,成功返回索引,失败,返回-1

    index = strings.LastIndexAny("hello world", "h&w&o")
    if index != -1 {
        fmt.Println("LastIndexAny exist :", index)
    } else {
        fmt.Println("LastIndexAny not exist")
    }

8:LastIndexByte(s string,c byte) int:返回字节c最后一次在父串str中出现的索引,成功,返回索引,失败,返回-1

    var c byte = 'o'
    index = strings.LastIndexByte("hello world", c)
    if index != -1 {
        fmt.Println("LastIndexByte exist :", index)
    } else {
        fmt.Println("LastIndexByte not exist")
    }

9:SplitN(s,sep string,n int)[]string:将字符串s按照sep子串划分成子串(子串不包含sep串)
(n >0 ):一直划分,直到划分到n个串
(n == 0):nil
(n < 0):划分到结束,返回所有子串

    str = "helloworld hellogolang helloprograming hellotest
     hellostring hello"
    sep := "hello"
    substring1 := strings.SplitN(str, sep, 0)
    fmt.Println("SplitN n == 0:", substring1)
    substring2 := strings.SplitN(str, sep, 4)
    fmt.Println("SplitN n > 0:", substring2)
    substring3 := strings.SplitN(str, sep, -1)
    fmt.Println("SplitN n < 0:", substring3)

10:SplitAfterN(s,sep string,n int)[]stsring:将字符串s按照sep之后划分成子串(子串包含sep串)
(n >0 ):一直划分,直到划分到n个串
(n == 0):nil
(n < 0):划分到结束,返回所有子串

    str = "helloworld hellogolang helloprograming hellotest
     hellostring hello"
    sep := "hello"
    substring4 := strings.SplitAfterN(str, sep, 0)
    fmt.Println("SplitAfterN n == 0:", substring4)
    substring5 := strings.SplitAfterN(str, sep, 4)
    fmt.Println("SplitAfterN n > 0:", substring5)
    substring6 := strings.SplitAfterN(str, sep, -1)
    fmt.Println("SplitAfterN n < 0 :", substring6)

11:Split(s,sep string)[]string:将字符串s按照sep划分成子串,直到划分结束,等同于SplitN(s,sep string,-1)

    str = "helloworld hellogolang helloprograming hellotest
     hellostring hello"
    sep := "hello"
    substring7 := strings.Split(str, sep)
    fmt.Println("Split :", substring7)

12:SplitAfter(s,sep string)[]string:将字符串按照sep之后划分成子串,直到划分结束返回所有子串等同于SplitAfterN(s,sep,-1)

    str = "helloworld hellogolang helloprograming hellotest
     hellostring hello"
    sep := "hello"
    substring8 := strings.SplitAfter(str, sep)
    fmt.Println("SplitAfter :", substring8)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值