Skip to content

Instantly share code, notes, and snippets.

@hungdoansy
Forked from nvtuan305/normalize_string.go
Created June 29, 2022 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hungdoansy/a9505c000308b8da8bbed42c350b51d2 to your computer and use it in GitHub Desktop.
Save hungdoansy/a9505c000308b8da8bbed42c350b51d2 to your computer and use it in GitHub Desktop.
golang: normalize string (include Vietnamese)
package main
import (
"fmt"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"strings"
"unicode"
)
func main() {
trans := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.String(trans, "Á|đ|Đ|Â|à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễì|í|ị|ỉ|ĩ|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|ỳ|ý|ỵ|ỷ|ỹ")
result = strings.ReplaceAll(result, "đ", "d")
result = strings.ReplaceAll(result, "Đ", "D")
fmt.Println(result)
// Should print: A|d|D|A|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|e|e|e|e|e|e|e|e|e|e|ei|i|i|i|i|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|u|u|u|u|u|u|u|u|u|u|u|y|y|y|y|y
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment