GoLang String - LastIndex()

In this tutorial, we will learn to find index position of particular last instance of a character or substring using LastIndex() method.

In the Go Language LastIndex() method is used to return the index position of particular last instance of a character or substring present in the actual string.

let us explore the uses case with help of simple examples.

String LastIndex()

LastIndex() is the method available in strings package which will return the index position of particular last instance of a character or substring present in the actual string. If substring not found, then -1 is returned. Indexing starts from 0.

Syntax:

CopiedCopy Code
strings.LastIndex(actual_string,sub_string/character)

It takes two parameters.

Parameters:

actual_string is the string and sub_string is the string in which its last instance index is returned. It is important to specify the "strings" package in import.

Example 1:

Let us consider the string - "welcome to gkindex" and return the last index of some characters.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // return the LastIndex of "e".
     fmt.Println(strings.LastIndex(actual_string,"e"))
     
     // return the LastIndex of "o".
     fmt.Println(strings.LastIndex(actual_string,"o"))
     
     // return the LastIndex of "x".
     fmt.Println(strings.LastIndex(actual_string,"x"))
     
     // return the LastIndex of "M".
     fmt.Println(strings.LastIndex(actual_string,"M"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
16
9
17
-1

Explanation for the above example:

  1. last index of "e". Its last instance position is 16
  2. last index of "o". Its last instance position is 9
  3. last index of "x". Its last instance position is 17
  4. last index of "M". It is not found in the actual_string. so, -1 is returned.

Example 2:

Let us consider the string - "welcome to gkindex, welcome to java, java is best language and welcome to python" and return the index of some substrings.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex, welcome to java, java is best language and welcome to python"
 
    fmt.Println("String: ", actual_string)

      // return the LastIndex of "welcome".
     fmt.Println(strings.LastIndex(actual_string,"welcome"))
     
     // return the LastIndex of "to".
     fmt.Println(strings.LastIndex(actual_string,"to"))
     
     // return the LastIndex of "java".
     fmt.Println(strings.LastIndex(actual_string,"java"))
     
     // return the LastIndex of "odd".
     fmt.Println(strings.LastIndex(actual_string,"odd"))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex, welcome to java, java is best language and welcome to python
63
71
37
-1

Explanation for the above example:

  1. Returning the last index of "welcome". Its last instance position is 63 in which the substring starts with "welcome".
  2. Returning the last index of "to". Its last instance position is 71 in which the substring starts with "to".
  3. Returning the last index of "java". Its last instance position is 37 in which the substring starts with "java".
  4. Returning the last index of "odd". It is not found in the actual_string. so, -1 is returned.

Conclusion

Now we know how to return the index position of particular last instance of a character or substring present in the actual string using the LastIndex() method and understood the implementation with working examples.