Home >Backend Development >Golang >How Can I Remove Diacritics from Strings in Go?
Removing Diacritics in Go
Eliminating diacritics (accent marks) from UTF-8 encoded strings is a common text processing task. Go provides several libraries for this purpose, as part of its Text normalization utilities.
One approach involves combining multiple libraries, as demonstrated below:
package main import ( "fmt" "unicode" "golang.org/x/text/transform" "golang.org/x/text/unicode/norm" ) // isMn determines if a rune represents a nonspacing mark (diacritic). func isMn(r rune) bool { return unicode.Is(unicode.Mn, r) } func main() { // Create a transformation chain to: // - Decompose the string into its unicode normalization form (NFD). // - Remove all nonspacing marks (diacritics). // - Recompose the string into its normalized form (NFC). t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC) // Apply the transformation to the input string "žůžo". result, _, _ := transform.String(t, "žůžo") // Print the resulting string, which is "zuzo" without diacritics. fmt.Println(result) }
The above is the detailed content of How Can I Remove Diacritics from Strings in Go?. For more information, please follow other related articles on the PHP Chinese website!