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.

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
Loading...

All contributors

Contribute to Docs

Learn Go on Codecademy