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

La función strings.TrimRightFunc() devuelve una porción de la string s con todos los puntos de código Unicode finales c que satisfacen f(c) eliminados.

Sintaxis:

func TrimRightFunc(s string, f func(rune) bool) string

Aquí, s es la string y func() es el método que satisface los caracteres de la string.

Valor de retorno: devuelve la string después de eliminar los caracteres finales de la string.

Ejemplo 1:

// Golang program to illustrate
// the strings.TrimRightFunc() Function
package main
   
// importing fmt, unicode and strings
import (
    "fmt"
    "strings"
    "unicode"
)
   
// Calling Main method
func main() {
   
    // Here we have a string. The function
    // returns true for the letters
    // and all other will trim out
    // from the string only from Right
    fmt.Print(strings.TrimRightFunc("77GeeksForGeeks!!!", func(r rune) bool {
        return !unicode.IsLetter(r)
    }))
}

Producción:

77GeeksForGeeks

Ejemplo 2:

// Golang program to illustrate
// the strings.TrimRightFunc() Function
package main
   
// importing fmt, unicode and strings
import (
    "fmt"
    "strings"
    "unicode"
)
   
// Calling Main method
func main() {
   
    // Here we have a string. The function
    // returns true for the letters
    // and numbers as well
    // and all other will trim out
    // from the string only from left
    fmt.Print(strings.TrimRightFunc("!!!34GeeksForGeeks!!!!", func(r rune) bool {
        return !unicode.IsLetter(r) && !unicode.IsNumber(r)
    }))
}

Producción:

!!!34GeeksForGeeks

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 *