LastIndex()

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.

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.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Go on Codecademy