首页 > golang > 如何从golang的字符串中找到表情符号?
2022
01-19

如何从golang的字符串中找到表情符号?

问题


我想知道表情符号是否存在,并替换为字符串(HTML unicode)。(符文到字符串)

例如,这是一个句子

i like you hahahah 😀 hello.

这就是结果。

i like you hahahah 😀 hello.

表情符号和表情符号位置是随机的。


解决方案

我们可以将字符串转换为[]rune,并将每个符文转换为ASCII或HTML实体

package main

import (
   "fmt"
   "strconv"
)

func main() {
   inp := "i like you hahahah 😀 hello."

   res := ""
   runes := []rune(inp)

   for i := 0; i < len(runes); i++ {
      r := runes[i]
      if r < 128 {
         res += string(r)
      } else {
         res += "&#" + strconv.FormatInt(int64(r), 10) + ";"
      }
   }

   fmt.Printf("result html string: %v", res)
}


本文》有 0 条评论

留下一个回复