Menu Close

Go – How to get the index of the last instance of sub string in go

Here, We will learn how to get the index of the last instance of sub string in go. We can get it by using LastIndex() function in strings package in go golang.

Function prototype:

func LastIndex(str, substr string) int

Return value:

LastIndex() function in strings package returns 
the index of the last instance of sub string
in str, or -1 if sub string is not present in str.

Example with code:

package main

import (
 "fmt"
  "strings"
)

func main() {

  str := strings.Index("Baby singh", "ing")
  fmt.Println(str)

  str = strings.Index("go golang", "go")
  fmt.Println(str)

  str = strings.Index("techie tooie", "to")
  fmt.Println(str)

  str = strings.Index("hello hi", " ")
  fmt.Println(str)
}

Output:

7
0
7
5

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, strings

Leave a Reply

Your email address will not be published.

Contact Us