How to find the last index value of specified string in Golang?
In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
In Go strings, you can find the last index value of the specified string from the original string using the following functions. These functions are defined under strings package so, you have to import strings package in your program for accessing these functions:
1. LastIndex: This function is used to find the index value of the last instance of the given string from the original string. If the given string is not available in the original string, then this method will return -1.
Syntax:
func LastIndex(str, sbstr string) int
Here, str is the original string and sbstr is a string, whose we want to find the last index value.
Example:
// Go program to illustrate how to find // the last index value of the given string package main import ( "fmt" "strings" ) // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of w3wiki" 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 last index value of the given string // Using LastIndex() function res1 := strings.LastIndex(str1, "e" ) res2 := strings.LastIndex(str2, "do" ) res3 := strings.LastIndex(str3, "jk" ) res4 := strings.LastIndex( "w3wiki, Beginner" , "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) } |
Output:
String 1: Welcome to the online portal of w3wiki String 2: My dog name is Dollar String 3: I like to play Ludo Index values: Result 1: 42 Result 2: 3 Result 3: -1 Result 4: 18
2. LastIndexAny: This method returns the index of the last instance of any Unicode code point from chars in the original string. If the Unicode code point from chars is not available in the original string, then this method will return -1.
Syntax:
func LastIndexAny(str, charstr string) int
Here, str is the original string and charstr is a Unicode code point from chars whose we want to find the last index value.
Example:
// Go program to illustrate how to find the // last index value of the given string package main import ( "fmt" "strings" ) // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of w3wiki" 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 last index value of the given string // Using LastIndexAny() function res1 := strings.LastIndexAny(str1, "mewdv" ) res2 := strings.LastIndexAny(str2, "ll" ) res3 := strings.LastIndexAny(str3, "qzmj" ) res4 := strings.LastIndexAny( "w3wiki, Beginner" , "ee" ) // 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) } |
Output:
String 1: Welcome to the online portal of w3wiki String 2: My dog name is Dollar String 3: I like to play Ludo Index values: Result 1: 42 Result 2: 18 Result 3: -1 Result 4: 17
3. LastIndexByte: This function returns the index of the last instance of the given byte in the original string. If the given byte is not available in the original string, then this method will return -1.
Syntax:
func LastIndexByte(str string, b byte) int
Here, str is the original string and b is a byte, whose we want to find the last index value.
Example:
// Go program to illustrate how to find the // last index value of the given bytes package main import ( "fmt" "strings" ) // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of w3wiki" 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 last index value of the given bytes // Using LastIndexByte() function res1 := strings.LastIndexByte(str1, 'e' ) res2 := strings.LastIndexByte(str2, 'a' ) res3 := strings.LastIndexByte(str3, 'q' ) res4 := strings.LastIndexByte( "w3wiki, Beginner" , 's' ) // 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) } |
Output:
String 1: Welcome to the online portal of w3wiki String 2: My dog name is Dollar String 3: I like to play Ludo Index values: Result 1: 42 Result 2: 19 Result 3: -1 Result 4: 19
Contact Us