Created
October 19, 2022 14:03
-
-
Save vuduongtp/91806c7581c972ae78d0b9f3fc128175 to your computer and use it in GitHub Desktop.
Golang - Convert Vietnamese accented characters to non-accented (Chuyển Tiếng Việt có dấu sang Tiếng Việt không dấu bằng ngôn ngữ Go/Golang)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"regexp" | |
"strings" | |
) | |
func ConvertAccented(text string) string { | |
text = strings.ToLower(text) | |
var RegexpA = `à|á|ạ|ã|ả|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ` | |
var RegexpE = `è|ẻ|ẽ|é|ẹ|ê|ề|ể|ễ|ế|ệ` | |
var RegexpI = `ì|ỉ|ĩ|í|ị` | |
var RegexpU = `ù|ủ|ũ|ú|ụ|ư|ừ|ử|ữ|ứ|ự` | |
var RegexpY = `ỳ|ỷ|ỹ|ý|ỵ` | |
var RegexpO = `ò|ỏ|õ|ó|ọ|ô|ồ|ổ|ỗ|ố|ộ|ơ|ờ|ở|ỡ|ớ|ợ` | |
var RegexpD = `đ` | |
rega := regexp.MustCompile(RegexpA) | |
rege := regexp.MustCompile(RegexpE) | |
regi := regexp.MustCompile(RegexpI) | |
rego := regexp.MustCompile(RegexpO) | |
regu := regexp.MustCompile(RegexpU) | |
regy := regexp.MustCompile(RegexpY) | |
regd := regexp.MustCompile(RegexpD) | |
text = rega.ReplaceAllLiteralString(text, "a") | |
text = rege.ReplaceAllLiteralString(text, "e") | |
text = regi.ReplaceAllLiteralString(text, "i") | |
text = rego.ReplaceAllLiteralString(text, "o") | |
text = regu.ReplaceAllLiteralString(text, "u") | |
text = regy.ReplaceAllLiteralString(text, "y") | |
text = regd.ReplaceAllLiteralString(text, "d") | |
return text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment