strings.ToValidUTF8() Function in Golang is used to returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement string, which may be empty.
Syntax:
C
Output:
C
Output:
func ToValidUTF8(str, rep string) stringHere, str is string which may contain invalid UTF-8 and rep is the replacement string. Return Value: It returns the replaced string. Example 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", " "))
}
This is GeeksForGeeksExample 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"))
}
GeeksForGeeks