The unicode.To function in Golang is part of the unicode package and is used to convert a given rune to a specific case: uppercase, lowercase, or title case. This function allows you to programmatically change the case of a rune according to the Unicode standard, making it particularly useful for text processing tasks such as case normalization, formatting, or user input validation.
Table of Contents
- Introduction
unicode.ToFunction Syntax- Examples
- Basic Usage
- Converting a String to Uppercase
- Converting Mixed-Case Text to Title Case
- Real-World Use Case Example
- Conclusion
Introduction
The unicode.To function provides a flexible way to convert a rune to a specified case. Unlike functions like unicode.ToUpper or unicode.ToLower, which convert a rune to a predefined case, unicode.To allows you to specify the desired case conversion. This is especially useful when dealing with text that may require case-sensitive operations or formatting across different languages and scripts.
unicode.To Function Syntax
The syntax for the unicode.To function is as follows:
func To(_case int, r rune) rune
Parameters:
_case: The target case for the conversion. You can useunicode.UpperCase,unicode.LowerCase, orunicode.TitleCase.r: The rune (character) you want to convert.
Returns:
rune: The rune converted to the specified case. If the rune does not have a case equivalent, it returns the rune unchanged.
Examples
Basic Usage
This example demonstrates how to use unicode.To to convert a rune to uppercase.
Example
package main
import (
"fmt"
"unicode"
)
func main() {
r := 'g'
upper := unicode.To(unicode.UpperCase, r)
fmt.Printf("The uppercase equivalent of '%c' is '%c'.\n", r, upper)
}
Output:
The uppercase equivalent of 'g' is 'G'.
Explanation:
- The
unicode.Tofunction is used to convert the lowercase rune'g'to its uppercase equivalent'G'. - The conversion is specified using
unicode.UpperCase.
Converting a String to Uppercase
This example shows how to use unicode.To to convert all characters in a string to uppercase.
Example
package main
import (
"fmt"
"unicode"
)
func toUpperCase(input string) string {
var result []rune
for _, r := range input {
result = append(result, unicode.To(unicode.UpperCase, r))
}
return string(result)
}
func main() {
input := "Hello, World!"
output := toUpperCase(input)
fmt.Println("Uppercase string:", output)
}
Output:
Uppercase string: HELLO, WORLD!
Explanation:
- The
toUpperCasefunction iterates over the input string and converts each rune to uppercase usingunicode.To. - The entire string is transformed to uppercase.
Converting Mixed-Case Text to Title Case
This example demonstrates how to use unicode.To to convert mixed-case text to title case.
Example
package main
import (
"fmt"
"unicode"
"unicode/utf8"
)
func toTitleCase(input string) string {
var result []rune
first := true
for _, r := range input {
if first {
result = append(result, unicode.To(unicode.TitleCase, r))
first = false
} else {
result = append(result, unicode.To(unicode.LowerCase, r))
}
if unicode.IsSpace(r) {
first = true
}
}
return string(result)
}
func main() {
input := "hello, world! welcome to golang."
output := toTitleCase(input)
fmt.Println("Title case string:", output)
}
Output:
Title case string: Hello, World! Welcome To Golang.
Explanation:
- The
toTitleCasefunction converts the first rune of each word to title case and the rest to lowercase. - The result is a string where each word starts with a capital letter, commonly known as title case.
Real-World Use Case Example: Formatting User Input
Suppose you are building an application that requires consistent formatting of user names, where the first letter of each name should be in uppercase and the rest in lowercase.
Example: Formatting User Names
package main
import (
"fmt"
"unicode"
)
func formatUserName(input string) string {
var result []rune
first := true
for _, r := range input {
if first {
result = append(result, unicode.To(unicode.UpperCase, r))
first = false
} else {
result = append(result, unicode.To(unicode.LowerCase, r))
}
if unicode.IsSpace(r) {
first = true
}
}
return string(result)
}
func main() {
input := "jOhn doE"
formatted := formatUserName(input)
fmt.Println("Formatted user name:", formatted)
}
Output:
Formatted user name: John Doe
Explanation:
- The
formatUserNamefunction ensures that each word in the input string starts with an uppercase letter, with the remaining letters in lowercase. - The result is a consistently formatted user name.
Conclusion
The unicode.To function in Go is used for converting runes to specific cases, supporting uppercase, lowercase, and title case transformations. It is highly useful in text processing tasks where case conversion is necessary, such as formatting, normalization, or validation. Whether you’re working with simple text strings or complex internationalized data, unicode.To provides a reliable way to handle case conversions in your applications.