Golang unicode.To() Function with Examples

Golang | unicode.To() Function: Here, we are going to learn about the To() function of the unicode package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 18, 2021

unicode.To()

The To() function is an inbuilt function of the unicode package which is used to map the given rune r to the specified case (unicode.UpperCase, unicode.LowerCase, or unicode.TitleCase) i.e., the To() function converts the given rune value to the specified case.

It accepts two parameters (_case int, r rune) and returns the value mapped to the specified case.

Syntax:

func To(_case int, r rune) rune

Parameter(s):

  • _case : Specified case in which we have to map the given rune value, it can be unicode.UpperCase, unicode.LowerCase, or unicode.TitleCase .
  • r : Rune type value to be mapped in the specified case (_case).

Return Value:

The return type of the unicode.To() function is a rune, it returns the value mapped to the specified case.

Example 1:

// Golang program to demonstrate the
// example of unicode.To() Function

package main

import (
	"fmt"
	"unicode"
)

func main() {
	// constant with mixed type runes
	const mixed = "Hello, world!"

	fmt.Println("UpperCase:")
	for _, c := range mixed {
		fmt.Printf("%c", unicode.To(unicode.UpperCase, c))
	}
	fmt.Println()

	fmt.Println("LowerCase:")
	for _, c := range mixed {
		fmt.Printf("%c", unicode.To(unicode.LowerCase, c))
	}
	fmt.Println()

	fmt.Println("TitleCase:")
	for _, c := range mixed {
		fmt.Printf("%c", unicode.To(unicode.TitleCase, c))
	}
}

Output:

UpperCase:
HELLO, WORLD!
LowerCase:
hello, world!
TitleCase:
HELLO, WORLD!

Example 2:

// Golang program to demonstrate the
// example of unicode.ToLower() Function

package main

import (
	"fmt"
	"unicode"
)

func main() {
	fmt.Printf("%#U\n", unicode.ToLower('Q'))
	fmt.Printf("%#U\n", unicode.ToLower('q'))
	fmt.Printf("%#U\n", unicode.ToLower('R'))
	fmt.Printf("%#U\n", unicode.ToLower('Ä'))
}

Output:

U+0071 'q'
U+0071 'q'
U+0072 'r'
U+00E4 'ä'

Golang unicode Package »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.