doushaju4901
2019-05-06 07:53
浏览 248
已采纳

如何将字符串从unicode转换为html实体

I'm trying to convert an Arabic string like this:

ص

to this:

ص

Is there a way (or package) to do it in Go?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpu1908 2019-05-06 08:00
    已采纳

    That character is not special in HTML, so you can include it as-is in the output, just be sure to set the proper encoding of the document.

    Note that to escape special characters in strings, you may use html.EscapeString(). But because ص is not special in HTML, that will not change.

    If for some reason you do need to escape it, you may simply use the decimal representation of the rune:

    fmt.Println(html.EscapeString("ص"))
    fmt.Printf("&#%d;", 'ص')
    

    Outputs (try it on the Go Playground):

    ص
    ص
    
    打赏 评论

相关推荐 更多相似问题