字符串.ToValidUTF8() Golang 中的函数用于返回字符串s 的副本,其中每次运行的无效 UTF-8 字节序列被替换字符串替换,该字符串可能为空。
句法:
func ToValidUTF8(str, rep string) string
这里, str 是可能包含无效 UTF-8 的字符串,而 rep 是替换字符串。
返回值:它返回替换后的字符串。
示例 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 GeeksForGeeks
示例 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