How to convert "\\u0301" to "\u0301"

huangapple go评论69阅读模式
英文:

How to convert "\\u0301" to "\u0301"

问题

我遇到的问题是:
给定一个包含多个特殊子字符串的字符串,如:
"\u0301" "\b" "\t",等等

请将上述所有特殊字符串转换为"\u0301","\b","\t",等等

请注意,如果只是在这里删除一个反斜杠,
你将得到一个"\u0301"的纯文本,
而不是Unicode的重音符号。

逐个替换每个特殊字符串是一种解决方法:

str = strings.Replace(str, "\u0301","\u0301", -1)

是否有一种通用的解决方案适用于所有转义字符代码?

英文:

The problem that I met is that
Given a string contains several special substrings like:
"\\u0301" "\\b" "\\t",..etc

Please convert all special strings above into "\u0301","\b","\t", ..etc

Note that if just removing a backslash here,
you will get a plaintext of "\u0301",
instead an accent mark of Unicode.

A one-by-one solution is to replace each special string

str = strings.Replace(str, "\\u0301","\u0301", -1)

Is a general solution to all escape character codes?

答案1

得分: 1

如果您需要将包含转义的Unicode序列和控制序列的字节序列转换为Unicode字符串,您可以使用strconv.Unquote函数。

为此,将字节转换为字符串,对双引号和换行符进行转义,并在该字符串的开头和结尾添加双引号字符。

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	b := []byte{65, 92, 117, 48, 51, 48, 49, 9, 88, 10, 34, 65, 34}
	// 与下面的代码等效
	// b := []byte("A\\u0301\tX\n\"A\"")

	// 转换为字符串
	s := string(b)

	fmt.Println(s)
	fmt.Println("=========")

	// 转义双引号
	s = strings.ReplaceAll(s, "\"", "\\\"")
	// 转义换行符
	s = strings.ReplaceAll(s, "\n", "\\n")

	r, err := strconv.Unquote(`"` + s + `"`)
	if err != nil {
		panic(err)
	}
	fmt.Println(r)
	fmt.Println("=========")
}

输出:

A\u0301	X
"A"
=========
Á	X
"A"
=========

https://go.dev/play/p/WRsNGOT1zLR

英文:

If you need to convert a byte sequence that contains escaped Unicode sequences and control sequences to a Unicode string, then you can use strconv.Unquote function.

To do this, convert the bytes to a string, escape the double-quote and newline characters, and add double-quote characters at the beginning and end of this string.

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	b := []byte{65, 92, 117, 48, 51, 48, 49, 9, 88, 10, 34, 65, 34}
	// the same as
	// b := []byte("A\\u0301\tX\n\"A\"")

	// convert to string
	s := string(b)

	fmt.Println(s)
	fmt.Println("=========")

	// escape double quotes
	s = strings.ReplaceAll(s, "\"", "\\\"")
	// escape newlines
	s = strings.ReplaceAll(s, "\n", "\\n")

	r, err := strconv.Unquote(`"` + s + `"`)
	if err != nil {
		panic(err)
	}
	fmt.Println(r)
	fmt.Println("=========")
}

Output:

A\u0301	X
"A"
=========
Á	X
"A"
=========

https://go.dev/play/p/WRsNGOT1zLR

huangapple
  • 本文由 发表于 2023年5月2日 11:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76151615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定