Home >Backend Development >Golang >How to Normalize Non-ASCII Text Input to ASCII Using the strings.Map Function?
Handling non-ASCII characters in text input can be a challenge, especially when the goal is to normalize them to ASCII equivalents. A common issue arises when encountering curly quotes instead of straight quotes. While custom string replacements can address this issue, the standard library offers a more comprehensive solution.
The strings.Map function provides a mechanism for mapping runes (Unicode characters) to other runes. This approach offers a customizable and generic method for converting non-ASCII characters to ASCII equivalents.
In this case, the following code demonstrates how to use Map to normalize curly quotes to straight quotes:
<code class="go">package main import ( "fmt" "strings" ) func main() { data := "Hello “Frank” or ‹François› as you like to be ‘called’" fmt.Printf("Original: %s\n", data) cleanedData := strings.Map(normalize, data) fmt.Printf("Cleaned: %s\n", cleanedData) } func normalize(in rune) rune { switch in { case '“', '‹', '”', '›': return '"' case '‘', '’': return '\'' } return in }</code>
Original: Hello “Frank” or ‹François› as you like to be ‘called’ Cleaned: Hello "Frank" or "François" as you like to be 'called'
By utilizing the strings.Map function, it is possible to define custom mapping rules that handle various non-ASCII characters, ensuring that all input text is normalized to ASCII equivalents as needed.
The above is the detailed content of How to Normalize Non-ASCII Text Input to ASCII Using the strings.Map Function?. For more information, please follow other related articles on the PHP Chinese website!