How can I remove all diacritics from the given UTF8 encoded string using Go? e.g. transform the string "žůžo"
=> "zuzo"
. Is there a standard way?
相关问题
- Golang mongodb aggregation
- UrlEncodeUnicode and browser navigation errors
- How to flatten out a nested json structure in go
- how to install private repo using glide golang
- WebElement.getText() function and utf8
相关文章
- Can I run a single test in a suite?
- How to check if a request was cancelled
- Is it possible to implement an interface with unex
- Why is `'↊'.isnumeric()` false?
- How to display unicode in SVG?
- How to access value of first index of array in Go
- Spanish Characters in HTML Page Title
- Embedded Interface
You can use the libraries described in Text normalization in Go.
Here's an application of those libraries:
To expand a bit on the existing answer:
The internet standard for comparing strings of different character sets is called "PRECIS" (Preparation, Enforcement, and Comparison of Internationalized Strings in Application Protocols) and is documented in RFC7564. There is also a Go implementation at golang.org/x/text/secure/precis.
None of the standard profiles will do what you want, but it would be fairly straight forward to define a new profile that did. You would want to apply Unicode Normalization Form D ("D" for "Decomposition", which means the accents will be split off and be their own combining character), and then remove any combining character as part of the additional mapping rule, then recompose with the normalization rule. Something like this:
This lets you expand your comparison with more options later (eg. width mapping, case mapping, etc.)
It's also worth noting that removing accents is almost never what you actually want to do when comparing strings like this, however, without knowing your use case I can't actually make that assertion about your project. To prevent the proliferation of precis profiles it's good to use one of the existing profiles where possible. Also note that no effort was made to optimize the example profile.