Menu Close

Go – LastIndexFunc() function in strings package in go

Here, We will learn about LastIndexFunc() function in strings package in go.  LastIndexFunc() function is used to get the index into string of the last Unicode code point satisfying function(rune) given it as LastIndexFunc() function parameter in go golang.

Function prototype:

func LastIndexFunc(str string, 
fun_name func(rune) bool) int

Input Parameters:

str - string type
fun_name - Will execute for every rune of str

Return value:

LastIndexFunc() function in strings package 
returns the index into string of the last
Unicode code point satisfying fun(char rune)
else -1 if none do.

Example with code:

package main

import (
  "fmt"
  "strings"
  "unicode"
)

func main() {

  s := strings.LastIndexFunc("Hi 987", unicode.IsNumber)
  fmt.Println(s)

  s = strings.LastIndexFunc("765 Hi", unicode.IsNumber)
  fmt.Println(s)
	
  s = strings.LastIndexFunc("Golang", unicode.IsNumber)
  fmt.Println(s)
	
}

Output:

6
7
8
1

To learn more about golang, Please refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, strings

Leave a Reply

Your email address will not be published. Required fields are marked *