utf8.go 316 字节
Newer Older
Z
zhaoke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package stringUtils

import (
	"unicode/utf8"

	"golang.org/x/text/encoding/simplifiedchinese"
)

func Convert2Utf8IfNeeded(data string) string {
	if !utf8.Valid([]byte(data)) && IsGBK([]byte(data)) {
		newLine, _ := simplifiedchinese.GBK.NewDecoder().Bytes([]byte(data))
		data = string(newLine)
	}

	return data
}