在Go语言中,字符串不同于其他语言,如Java, c++, Python等。它是一个变宽字符序列,其中每个字符都使用UTF-8编码由一个或多个字节表示。
在Go字符串中, 可以使用以下函数从原始字符串中找到指定字符串的第一个索引值。这些函数是在字符串包下定义的, 因此, 你必须在程序中导入字符串包才能访问这些功能:
1.索引:此函数用于从原始字符串中查找给定字符串的第一个实例的索引值。如果给定的字符串在原始字符串中不可用, 则此方法将返回-1。
语法如下:
func Index(str, sbstr string) int
【如何在Golang中找到指定字符串的索引值()】这里, str是原始字符串, 并且str是一个我们要查找索引值的字符串。让我们借助示例来讨论这个概念:
例子:
//Go program to illustrate how to find
//the index value of the given string
package mainimport (
"fmt"
"strings"
)//Main function
func main() {//Creating and initializing the strings
str1 := "Welcome to the online portal of srcmini"
str2 := "My dog name is Dollar"
str3 := "I like to play Ludo"//Displaying strings
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2: " , str2)
fmt.Println( "String 3: " , str3)//Finding the index value of the given strings
//Using Index() function
res1 := strings.Index(str1, "Geeks" )
res2 := strings.Index(str2, "do" )
res3 := strings.Index(str3, "chess" )
res4 := strings.Index( "srcmini, geeks" , "ks" )//Displaying the result
fmt.Println( "\nIndex values:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2: " , res2)
fmt.Println( "Result 3: " , res3)
fmt.Println( "Result 4: " , res4)}
输出如下:
String 1:Welcome to the online portal of srcmini
String 2:My dog name is Dollar
String 3:I like to play LudoIndex values:
Result 1:32
Result 2:3
Result 3:-1
Result 4:3
2. IndexAny:此方法从原始字符串中的chars返回任何Unicode代码点的第一个实例的索引。如果来自chars的Unicode代码点在原始字符串中不可用, 则此方法将返回-1。
语法如下:
func IndexAny(str, charstr string) int
这里, str是原始字符串, 并且查斯特是chars的Unicode代码点, 我们想要查找索引值。
例子:
//Go program to illustrate how to find
//the index value of the given string
package mainimport (
"fmt"
"strings"
)//Main function
func main() {//Creating and initializing the strings
str1 := "Welcome to the online portal of srcmini"
str2 := "My dog name is Dollar"
str3 := "I like to play Ludo"//Displaying strings
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2: " , str2)
fmt.Println( "String 3: " , str3)//Finding the index value
//of the given strings
//UsingIndexAny() function
res1 := strings.IndexAny(str1, "G" )
res2 := strings.IndexAny(str2, "do" )
res3 := strings.IndexAny(str3, "lqxa" )
res4 := strings.IndexAny( "srcmini, geeks" , "uywq" )//Displaying the result
fmt.Println( "\nIndex values:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2: " , res2)
fmt.Println( "Result 3: " , res3)
fmt.Println( "Result 4: " , res4)}
输出如下:
String 1:Welcome to the online portal of srcmini
String 2:My dog name is Dollar
String 3:I like to play LudoIndex values:
Result 1:32
Result 2:3
Result 3:2
Result 4:-1
3. IndexByte:此函数返回原始字符串中给定字节的第一个实例的索引。如果给定的字节在原始字符串中不可用, 则此方法将返回-1。
语法如下:
func IndexByte(str string, b byte) int
这里, str是原始字符串, 并且b是一个字节, 我们要查找其索引值。让我们借助示例来讨论这个概念:
例子:
//Go program to illustrate how to find
//the index value of the given bytes
package mainimport (
"fmt"
"strings"
)//Main function
func main() {//Creating and initializing the strings
str1 := "Welcome to the online portal of srcmini"
str2 := "My dog name is Dollar"
str3 := "I like to play Ludo"//Displaying strings
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2: " , str2)
fmt.Println( "String 3: " , str3)//Finding the index value of the given bytes
//Using IndexByte() function
res1 := strings.IndexByte(str1, 'c' )
res2 := strings.IndexByte(str2, 'o' )
res3 := strings.IndexByte(str3, 'q' )
res4 := strings.IndexByte( "srcmini, geeks" , 'G' )//Displaying the result
fmt.Println( "\nIndex values:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2: " , res2)
fmt.Println( "Result 3: " , res3)
fmt.Println( "Result 4: " , res4)}
输出如下:
String 1:Welcome to the online portal of srcmini
String 2:My dog name is Dollar
String 3:I like to play LudoIndex values:
Result 1:3
Result 2:4
Result 3:-1
Result 4:0
推荐阅读
- help/Makefile.am:21: error: USE_NLS does not appear in AM_CONDITIONAL
- 如何找到关系的最高范式()
- 如何在Golang中找到指针的容量()
- 如何在C#中查找StringBuilder的容量(示例)
- 如何在不使用sizeof的情况下在C/C++中查找数组的大小()
- 如何使用计算反正切arc(实现示例)
- 如何在cmd(命令行)上执行C#程序()
- 如何有效地对20年代的大列表日期进行排序
- Python如何使用Pandas实现vLookup(代码实例)