strings.LastIndexAny() Function in Golang With Examples
strings.LastIndexAny() Function in the Golang is used to find the index of the last instance of any Unicode code point from chars in a given string. If the Unicode code point from chars is not found then it returns -1. Thus, this function returns an integer value. The index is counted taking zero as the starting index of the string.
func LastIndexAny(str, chars string) int
Here, str is the original string and charstr is a Unicode code point from chars whose we want to find the last index value.
10 -1 12
For the second output, character ‘g’ is not present so it displays -1 as a result. Note here that this function is case sensitive so it takes ‘G’ and ‘g’ differently.
For the first output the characters are ‘I’ and ‘i’. So the compiler will display the index of the last occurrence of either ‘I’ or ‘i’ and since ‘i’ comes last here so that will be the output. For the second output, the character to be searched is a space. Since there are two spaces in the given string so the output will be the index of the last space.