función strings.ToValidUTF8() en Golang con ejemplos

La función strings.ToValidUTF8() en Golang se usa para devolver una copia de la string s con cada ejecución de secuencias de bytes UTF-8 no válidas reemplazadas por la string de reemplazo, que puede estar vacía.

Sintaxis:

func ToValidUTF8(str, rep string) string

Aquí, str es una string que puede contener UTF-8 no válido y rep es la string de reemplazo.

Valor de retorno: Devuelve la string reemplazada.

Ejemplo 1:

// Golang program to show the usage
// of strings.ToValidUTF8() Function
package main
  
// importing fmt and strings
import (
    "fmt"
    "strings"
)
  
// calling main method
func main() {
  
    // There is no invalid UTF-8 character
    // present, so the same string returned
    fmt.Print(strings.ToValidUTF8("This is GeeksForGeeks", " "))
}

Producción:

This is GeeksForGeeks

Ejemplo 2:

// Golang program to show the usage
// of strings.ToValidUTF8() Function
package main
  
// importing fmt and strings
import (
    "fmt"
    "strings"
)
  
// calling main method
func main() {
  
    // Invalid UTF-8 '\xc5' replaced by 'For'
    fmt.Print(strings.ToValidUTF8("Geeks\xc5Geeks", "For"))
}

Producción:

GeeksForGeeks

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 *