中文标点转为英文标点-golang

业务中,中文标点符号常常是一个问题,在涉及到一些文本相关的逻辑时,为了避免复杂的逻辑,我们一般统一将中文标点转换为英文标点,再进行相关计算。

示例

var punctuationMap = map[rune]rune{
	8216: 39, // '
	8217: 39, // '
	8220: 34, // "
	8221: 34, // "
	12290: 46, // .
	12304: 91, // [
	12305: 93, // ]
	65281: 33, // !
	65288: 40, // (
	65289: 41, // )
	65292: 44, // ,
	65306: 58, // :
	65307: 59, // ;
	65311: 63, // ?
}
func PunctuationToEn(text string) string {
	text = strings.Map(func(r rune) rune {
		if v, ok := punctuationMap[r]; ok {
			return v
		}
		return r
	}, text)
	return text
}
func TestOther(t *testing.T) {
	text := "中国,还!在。?!,;:“”‘'()【】"
	t.Log(text)
	t.Log(PunctuationToEn(text))
}

以上是提供的常见的,在展现上中英文符号类似的一些中文标点符号的转换,输出结果为:

参考

https://www.cnblogs.com/jjjs/p/4763945.html

https://blog.csdn.net/lichaobxd/article/details/105773492

0 评论
最新
最旧 最多投票
内联反馈
查看所有评论
滚动至顶部