如何在Go中获取符文的十进制值?

我需要解析一些字符串并通过将任何特殊字符解析为 'â' TO â 来清理它们。这是十进制编码。我知道如何使用this将其解析为 Unicode ,但我需要十进制代码。整个想法是替换那些特殊字符,如果它们包含特殊字符,则用转换返回整个字符串。例如:


text := "chitâra"

text := parseNCRs(text) //can be by reference

parseNCRs(&text) //or passing the pointer

fmt.Println(text) //Outputs: "chitâra"


九州编程
浏览 168回答 1
1回答

qq_遁去的一_1

遍历字符串以获取符文的数值。func escape(s string) string {&nbsp; var buf bytes.Buffer&nbsp; for _, r := range s {&nbsp; &nbsp; if r > 127 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(&buf, "&#%d;", r)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteRune(r)&nbsp; &nbsp; }&nbsp; }&nbsp; return buf.String()}playground如果您要转义 HTML 或 XML,那么您还应该处理其他特殊字符:func escape(s string) string {&nbsp; var buf bytes.Buffer&nbsp; for _, r := range s {&nbsp; &nbsp; if r > 127&nbsp; || r == '<' || r == '>' || r == '&' || r == '"' || r = '\'' {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(&buf, "&#%d;", r)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteRune(r)&nbsp; &nbsp; }&nbsp; }&nbsp; return buf.String()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go