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
  • Save hungdoansy/a9505c000308b8da8bbed42c350b51d2 to your computer and use it in GitHub Desktop.
Save hungdoansy/a9505c000308b8da8bbed42c350b51d2 to your computer and use it in GitHub Desktop.

Revisions

  1. @nvtuan305 nvtuan305 created this gist Dec 4, 2020.
    19 changes: 19 additions & 0 deletions normalize_string.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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
    }