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

La función strings.TrimFunc() en Golang se usa para devolver una porción de la string s con todos los puntos de código Unicode iniciales y finales c que satisfacen f(c) eliminados.

Sintaxis:

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

Aquí, s es la string y la función verifica el valor de la runa y devuelve verdadero si se puede recortar.

Tipo de devolución: devuelve la string después de eliminar los caracteres especificados de la string.

Ejemplo 1:

// Golang program to illustrate
// the strings.TrimFunc() 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
    fmt.Print(strings.TrimFunc("77GeeksForGeeks!!!", func(r rune) bool {
        return !unicode.IsLetter(r)
    }))
}

Producción:

GeeksForGeeks

Ejemplo 2:

// Golang program to illustrate
// the strings.TrimFunc() 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
    fmt.Print(strings.TrimFunc("1234GeeksForGeeks!!!!", func(r rune) bool {
        return !unicode.IsLetter(r) && !unicode.IsNumber(r)
    }))
}

Producción:

1234GeeksForGeeks

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 *