Golang unicode Package

Golang | unicode Package: Here, we are going to learn about the Golang unicode package, its constants, variables, and functions with examples.

In Go language, the unicode package provides data and functions to test some properties of Unicode code points.

To use these unicode package constants, variables, and functions – we need to import the unicode package.

The import statement:

import "unicode"

List of Golang unicode Package Constants, Variables, and Functions

The unicode package has the following constants, variables, and functions,

Constants

Constant Description Value
unicode.MaxRune It returns the maximum valid Unicode code point. '\U0010FFFF'
unicode.ReplacementChar It represents invalid code points. '\uFFFD'
unicode.MaxASCII It returns the maximum ASCII value. '\u007F'
unicode.MaxLatin1 It returns the maximum Latin-1 value. '\u00FF'
unicode.UpperCase It returns the index into the delta arrays inside CaseRanges for case mapping. 0
unicode.LowerCase It returns the index into the delta arrays inside CaseRanges for case mapping. 1
unicode.TitleCase It returns the index into the delta arrays inside CaseRanges for case mapping. 2
unicode.MaxCase It returns the index into the delta arrays inside CaseRanges for case mapping. 3
unicode.Version It returns the version which is the Unicode edition from which the tables are derived. "13.0.0"

Functions

Function Description
unicode.In() It checks whether the given rune is a member of one of the given ranges.
unicode.Is() It checks whether the given rune is a member of the specified table of ranges.
unicode.IsControl() It checks whether the given rune is a control character. Control characters are defined under C (other) Unicode category.
unicode.IsDigit() It checks whether the given rune r is a decimal digit.
unicode.IsGraphic() It checks whether the given rune r is defined as a Graphic by Unicode. These characters include all types of letters, marks, numbers, punctuation, symbols, and spaces, from the categories L, M, N, P, S, Zs.
unicode.IsLetter() It checks whether the given rune r is a letter (category L – the set of Unicode letters).
unicode.IsLower() It checks whether the given rune r is a lower case letter.
unicode.IsMark() It checks whether the given rune r is a mark character (category M – the set of Unicode mark characters).
unicode.IsNumber() It checks whether the given r rune is a number (category N – the set of Unicode number characters).
unicode.IsOneOf() It checks whether the given rune r is a member of one of the ranges (given RangeTable).
unicode.IsPrint() It checks whether the given rune r is defined as a printable character by Go Language. These characters include all letters, marks, numbers, punctuation, symbols, and the ASCII space characters, from the categories L, M, N, P, S, and the ASCII space character.
unicode.IsPunct() It checks whether the given rune r is a Unicode punctuation character (category P – the set of Unicode punctuation characters).
unicode.IsSpace() It checks whether the given rune r is a space character as defined by Unicode's White Space property.
unicode.IsSymbol() It checks whether the given rune is a symbolic character.
unicode.IsTitle() It checks whether the given rune r is a title case letter.
unicode.IsUpper() It checks whether the given rune r is an uppercase letter.
unicode.SimpleFold() It iterates over Unicode code points equivalent under the Unicode-defined simple case folding. Among the code points equivalent to rune (including rune itself), SimpleFold() function returns the smallest rune > r if one exists, or else the smallest rune >= 0. If r is not a valid Unicode code point, SimpleFold(r) returns r.
unicode.To() It maps 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.
unicode.ToLower() It maps the given rune r to lowercase i.e., the ToLower() function converts the given rune value to lowercase.
unicode.ToTitle() It maps the given rune r to the title case i.e., the ToTitle() function converts the given rune value to the title case.
unicode.ToUpper() It maps the given rune r to uppercase i.e., the ToUpper() function converts the given rune value to uppercase.

Example of unicode.In() function

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

package main

import (
	"fmt"
	"unicode"
)

func main() {
	fmt.Println("unicode.In('x', unicode.Latin):",
		unicode.In('x', unicode.Latin))

	fmt.Println("unicode.In('F', unicode.ASCII_Hex_Digit):",
		unicode.In('F', unicode.ASCII_Hex_Digit))

	fmt.Println("unicode.In('\\t', unicode.White_Space):",
		unicode.In('\t', unicode.White_Space))
		
	fmt.Println("unicode.In('\\a', unicode.White_Space):",
		unicode.In('\a', unicode.White_Space))
}

Output:

unicode.In('x', unicode.Latin): true
unicode.In('F', unicode.ASCII_Hex_Digit): true
unicode.In('\t', unicode.White_Space): true
unicode.In('\a', unicode.White_Space): false

Example of unicode.To() function

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

package main

import (
	"fmt"
	"unicode"
)

func main() {
	var r rune

	r = 'Q'
	fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, r))
	fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, r))
	fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, r))
	fmt.Println()

	r = 'q'
	fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, r))
	fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, r))
	fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, r))
	fmt.Println()

	r = 'Ä'
	fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, r))
	fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, r))
	fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, r))
}

Output:

U+0051 'Q'
U+0071 'q'
U+0051 'Q'

U+0051 'Q'
U+0071 'q'
U+0051 'Q'

U+00C4 'Ä'
U+00E4 'ä'
U+00C4 'Ä'

Example of unicode.ToUpper() function

// Golang program to demonstrate the
// example of unicode.ToUpper() 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.ToUpper(c))
	}
}

Output:

UpperCase:
HELLO, WORLD!



Comments and Discussions!

Load comments ↻






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