Función strings.LastIndexFunc() en Golang con ejemplos

La función strings.LastIndexFunc() en Golang devuelve el índice de la string s del último punto de código Unicode, que satisface func(c), o devuelve -1 si no se encuentran coincidencias.

Sintaxis:

func LastIndexFunc(s string, f func(rune) bool) int

Aquí, s es la string y func() es una función.

Valor devuelto: Devuelve el entero.

Ejemplo 1:

// Golang program to illustrate the
// strings.LastIndexFunc() Function
package main
  
// importing fmt, strings and unicode
import (
    "fmt"
    "strings"
    "unicode"
)
  
// calling main method
func main() {
  
    // returns the last index of number.
    fmt.Println(strings.LastIndexFunc("GeeksForGeeks123", unicode.IsNumber))
  
    // returns the last index of number.
    fmt.Println(strings.LastIndexFunc("23Geeks1", unicode.IsNumber))
}

Producción:

15
7

Ejemplo 2:

// Golang program to illustrate the
// strings.LastIndexFunc() Function
package main
  
// importing fmt, strings and unicode
import (
    "fmt"
    "strings"
    "unicode"
)
  
// calling main method
func main() {
  
    // returns the last index of letter.
    fmt.Println(strings.LastIndexFunc("GeeksForGeeks123", unicode.IsLetter))
  
    // returns the last index of letter.
    fmt.Println(strings.LastIndexFunc("23Geeks1", unicode.IsLetter))
}

Producción:

12
6

Publicación traducida automáticamente

Artículo escrito por PranchalKatiyar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *