Created
January 31, 2016 08:21
-
-
Save tkrajina/d423e9b9fc2e72d63072 to your computer and use it in GitHub Desktop.
Golang remove accents
This file contains 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
package main | |
import ( | |
"fmt" | |
"unicode" | |
"golang.org/x/text/transform" | |
"golang.org/x/text/unicode/norm" | |
) | |
func isMn(r rune) bool { | |
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks | |
} | |
func main() { | |
s := "Yoùr Śtring šđč枊ĐČĆŽ Ötzi's Nationalität èàì" | |
b := make([]byte, len(s)) | |
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC) | |
_, _, e := t.Transform(b, []byte(s), true) | |
if e != nil { | |
panic(e) | |
} | |
fmt.Println(string(b)) | |
} |
sorry the Ñ is a not character accent in spanish
this works but takes the Ñ as an accent char so does not work in Spanish
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little late to the party but it looks like that function is now deprecated. You can use
runes.Remove
instead:https://play.golang.org/p/ZcSR45sdlCh