English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Go言語の文字列では、以下の関数を使用して原始文字列から指定された文字列の最初のインデックス値を見つけることができます。これらの関数はstringsパッケージで定義されているため、これらの機能を使用するには、プログラムに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関数 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