How to find the last index value of specified string in Golang? - GeeksforGeeks

26 Aug, 2019
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 stringpackage main  import (    "fmt"    "strings")  // Main functionfunc main() {      // Creating and initializing the strings    str1 := "Welcome to the online portal of GeeksforGeeks"    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("GeeksforGeeks, 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)  }
Output:
String 1:  Welcome to the online portal of GeeksforGeeks
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 stringpackage main  import (    "fmt"    "strings")  // Main functionfunc main() {      // Creating and initializing the strings    str1 := "Welcome to the online portal of GeeksforGeeks"    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("GeeksforGeeks, geeks", "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 GeeksforGeeks
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 bytespackage main  import (    "fmt"    "strings")  // Main functionfunc main() {      // Creating and initializing the strings    str1 := "Welcome to the online portal of GeeksforGeeks"    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("GeeksforGeeks, geeks", '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 GeeksforGeeks
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

Golang
Golang-String
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
strings.Replace() Function in Golang With Examples
Arrays in Go
Golang Maps
How to Trim a String in Golang?
How to convert a string in lower case in Golang?
Different Ways to Find the Type of Variable in Golang
Slices in Golang
How to Parse JSON in Golang?
How to compare times in Golang?
Inheritance in GoLang