阅读 578

Go 语言查找字符串索引值

在Go语言字符串中,可以使用以下函数从原始字符串中找到指定字符串的第一个索引值。这些函数是在字符串包下定义的,因此,您必须在程序中导入strings包才能使用这些功能:

1.Index:此函数用于从原始字符串中查找给定字符串的第一个实例的索引值。如果给定的字符串在原始字符串中不存在,则此方法将返回-1。

语法:

func Index(str, sbstr string) int

在这里,str是原始字符串,sbstr是我们要查找索引值的字符串。让我们借助示例来讨论这个概念:

示例

//给定字符串的索引值package mainimport (
    "fmt"    "strings")func main() {

    //创建和初始化字符串    str1 := "Welcome to the online portal of nhooo"    str2 := "My dog name is Dollar"    str3 := "I like to play Ludo"    //显示字符串    fmt.Println("字符串 1: ", str1)
    fmt.Println("字符串 2: ", str2)
    fmt.Println("字符串 3: ", str3)

    //查找给定字符串的索引值    //使用Index()函数    res1 := strings.Index(str1, "Geeks")
    res2 := strings.Index(str2, "do")
    res3 := strings.Index(str3, "chess")
    res4 := strings.Index("nhooo, geeks", "ks")

    //显示结果    fmt.Println("\n索引值:")
    fmt.Println("结果 1: ", res1)
    fmt.Println("结果 2: ", res2)
    fmt.Println("结果 3: ", res3)
    fmt.Println("结果 4: ", res4)

}

输出:

字符串 1:  Welcome to the online portal of nhooo字符串 2:  My dog name is Dollar字符串 3:  I like to play Ludo索引值:结果 1:  -1结果 2:  3结果 3:  -1结果 4:  10

2. IndexAny:此方法从原始字符串中的chars返回任何Unicode码的第一个实例的索引值。如果原始字符中没有来自chars的Unicode代码点,则此方法将返回-1。

语法:

func IndexAny(str, charstr string) int

在这里,str是原始字符串,charstr是chars的Unicode代码点,我们想要查找索引值。

示例

//给定字符串的索引值package mainimport (
    "fmt"    "strings")func main() {

    //创建和初始化字符串    str1 := "Welcome to the online portal of nhooo.com"    str2 := "My dog name is Dollar"    str3 := "I like to play Ludo"    //显示字符串    fmt.Println("字符串 1: ", str1)
    fmt.Println("字符串 2: ", str2)
    fmt.Println("字符串 3: ", str3)

    //查找给定的字符串索引值    //使用IndexAny()函数    res1 := strings.IndexAny(str1, "G")
    res2 := strings.IndexAny(str2, "do")
    res3 := strings.IndexAny(str3, "lqxa")
    res4 := strings.IndexAny("nhooo, geeks", "uywq")

    //显示结果    fmt.Println("\n索引值:")
    fmt.Println("结果 1: ", res1)
    fmt.Println("结果 2: ", res2)
    fmt.Println("结果 3: ", res3)
    fmt.Println("结果 4: ", res4)

}

输出:

字符串 1:  Welcome to the online portal of nhooo.com字符串 2:  My dog name is Dollar字符串 3:  I like to play Ludo索引值:结果 1:  -1结果 2:  3结果 3:  2结果 4:  -1

3. IndexByte:此函数返回原始字符串中给定字节的第一个实例的索引。如果给定的字节在原始字符串中不存在,则此方法将返回-1。

语法:

func IndexByte(str string, b byte) int

在这里,str是原始字符串,b是一个字节,我们要查找其索引值。让我们借助示例来讨论这个概念:

示例

// 给定字节的索引值package mainimport (
    "fmt"    "strings")// Main functionfunc main() {

    //创建和初始化字符串    str1 := "Welcome to the online portal of nhooo.com"    str2 := "My dog name is Dollar"    str3 := "I like to play Ludo"    // 显示字符串    fmt.Println("字符串 1: ", str1)
    fmt.Println("字符串 2: ", str2)
    fmt.Println("字符串 3: ", str3)

    //查找给定字节的索引值    //使用IndexByte()函数    res1 := strings.IndexByte(str1, 'c')
    res2 := strings.IndexByte(str2, 'o')
    res3 := strings.IndexByte(str3, 'q')
    res4 := strings.IndexByte("nhooo, geeks", 'G')

    //显示结果    fmt.Println("\n索引值:")
    fmt.Println("结果 1: ", res1)
    fmt.Println("结果 2: ", res2)
    fmt.Println("结果 3: ", res3)
    fmt.Println("结果 4: ", res4)

}

输出:

字符串 1:  Welcome to the online portal of nhooo字符串 2:  My dog name is Dollar字符串 3:  I like to play Ludo索引值:结果 1:  3结果 2:  4结果 3:  -1结果 4:  0


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐