×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang strings.LastIndexAny() Function with Examples

Golang | strings.LastIndexAny() Function: Here, we are going to learn about the LastIndexAny() function of the strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 17, 2021

strings.LastIndexAny()

The LastIndexAny() function is an inbuilt function of strings package which is used to get the index of the last occurrence (instance) of any Unicode code point from substring in the string. It accepts two parameters – string and substring (chars) and returns the last index, or -1 if no Unicode code point from substring is not present in the string.

Syntax

func LastIndexAny(str, chars string) int

Parameters

  • str : String in which we have to check the Unicode code point from the substring.
  • substr : Substring to be checked in the string.

Return Value

The return type of the LastIndexAny() function is an int, it returns the index of the last occurrence (instance) of any Unicode code point from substring in the string, or -1 if no Unicode code point from substring is present in the string.

Example 1

// Golang program to demonstrate the
// example of strings.LastIndexAny() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.LastIndexAny("Hello, world", "aeoiu"))
	fmt.Println(strings.LastIndexAny("Hmmm", "aeoiu"))
	fmt.Println(strings.LastIndexAny("Okay", "oy"))
}

Output:

8
-1
3

Example 2

// Golang program to demonstrate the
// example of strings.LastIndexAny() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str string
	var substr string
	var index int

	str = "Hello, world!"
	substr = "aeoiu"

	index = strings.LastIndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Last index of any chars from %q in %q is %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}

	str = "Hello, world!"
	substr = "pqx"

	index = strings.LastIndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Last index of any chars from %q in %q is %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}

	str = "Hello! Hi! How are you?"
	substr = "@!?"

	index = strings.LastIndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Last index of any chars from %q in %q is %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}

	str = "Hello! Hi! How are you?"
	substr = ".<>*&"

	index = strings.LastIndexAny(str, substr)
	if index >= 0 {
		fmt.Printf("Last index of any chars from %q in %q is %d index.\n", substr, str, index)
	} else {
		fmt.Printf("No chars from %q does not found in %q.\n", substr, str)
	}
}

Output:

Last index of any chars from "aeoiu" in "Hello, world!" is 8 index.
No chars from "pqx" does not found in "Hello, world!".
Last index of any chars from "@!?" in "Hello! Hi! How are you?" is 22 index.
No chars from ".<>*&" does not found in "Hello! Hi! How are you?".

Golang strings Package Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.