Go LastIndex()

jameskeezer's avatar
Published Sep 26, 2023
Contribute to Docs

The LastIndex() function returns the index value of the last occurrence of a substring in the original string. Otherwise, it returns -1 if the substring is not present in the original string.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours

Syntax

strings.LastIndex(str, substr)

Where str is the original string and substr is the substring to find in the original string.

Example

The following example demonstrates the use of the strings.LastIndex() function:

package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.LastIndex("flibbertigibbet ", "bbe"))
}

The output will be:

11

Codebyte Example

The provided example is executable and implements the strings.LastIndex() function.

Code
Output

All contributors

Contribute to Docs

Learn Go on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours