English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang 기본 강의

Golang 제어 문

Golang 함수 & 메서드

Golang 구조체

Golang 슬라이스 & 배열

Golang 문자열(String)

Golang 포인터

Golang 인터페이스

Golang 동기

Golang 예외(Error)

Golang 다른杂项

Go 언어 문자열 인덱스 값 찾기

Go 언어 문자열에서 원본 문자열에서 지정된 문자열의 첫 번째 인덱스 값을 찾을 수 있는 다음 함수를 사용할 수 있습니다. 이 함수들은 문자열 패키지 아래에 정의되어 있으며, 이 기능을 사용하려면 프로그램에 strings 패키지를 가져오아야 합니다:

1.Index:이 함수는 원본 문자열에서 주어진 문자열의 첫 번째 인스턴스의 인덱스 값을 찾습니다. 주어진 문자열이 원본 문자열에 없으면 이 메서드는-1。

语法:

func Index(str, sbstr string) int

在这里,str是原始字符串,sbstr은 우리가 인덱스 값을 찾으려는 문자열입니다. 이 개념에 대해 예제를 통해 설명해 보겠습니다:

//给定字符串的索引值
package main
import (
    "fmt"
    "strings"
)
func main() {
    //创建和初始化字符串
    str1 := "Welcome to the online portal of w3codebox"
    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("w3codebox, geeks", "ks")
    //显示结果
    fmt.Println("\n索引值:")
    fmt.Println("结果 1: ", res1)
    fmt.Println("结果 2: ", res2)
    fmt.Println("结果 3: ", res3)
    fmt.Println("结果 4: ", res4)
}

输出:

字符串 1Welcome to the online portal of w3codebox
字符串 2My dog name is Dollar
字符串 3I 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 main
import (
    "fmt"
    "strings"
)
func main() {
    //创建和初始化字符串
    str1 := "Welcome to the online portal of oldtoolbag.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("w3codebox, geeks", "uywq")
    //显示结果
    fmt.Println("\n索引值:")
    fmt.Println("结果 1: ", res1)
    fmt.Println("结果 2: ", res2)
    fmt.Println("结果 3: ", res3)
    fmt.Println("结果 4: ", res4)
}

输出:

字符串 1Welcome to the online portal of oldtoolbag.com
字符串 2My dog name is Dollar
字符串 3I 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 main
import (
    "fmt"
    "strings"
)
// Main function
func main() {
    //创建和初始化字符串
    str1 := "Welcome to the online portal of oldtoolbag.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("w3codebox, geeks", 'G')
    //显示结果
    fmt.Println("\n索引值:")
    fmt.Println("结果 1: ", res1)
    fmt.Println("结果 2: ", res2)
    fmt.Println("结果 3: ", res3)
    fmt.Println("结果 4: ", res4)
}

输出:

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