Home > Backend Development > Golang > How Can I Remove Accents from Strings in Go?

How Can I Remove Accents from Strings in Go?

Patricia Arquette
Release: 2024-11-03 15:45:03
Original
765 people have browsed it

How Can I Remove Accents from Strings in Go?

Removing Accents from Strings in Go

Problem

Converting accented characters into their non-accented equivalents has been a challenge for some Go programmers. An attempt to implement a function using the "code.google.com/p/go.text/unicode/norm" package in Go 1.4 proved unsuccessful.

Solution

As of April 2015, an alternative approach is available with the introduction of the "runes" package, which includes a "Remove" function specifically designed for this purpose.

Go 1.5/1.6 Update

Looking ahead, Go 1.5 or 1.6 will likely bring a new "runes" package with transformation operations. This will offer a simplified solution using the "Remove" function, as seen in the following example:

<code class="go">package main

import (
    "fmt"
    "transform"

    "github.com/kjk/runes"
    "github.com/kjk/runes/example_test"
)

func ExampleRemove() {
    t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
    s, _, _ := transform.String(t, "résumé")
    fmt.Println(s)

    // Output:
    // resume
}

func main() {
    ExampleRemove()
}</code>
Copy after login

The above is the detailed content of How Can I Remove Accents from Strings in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template