core/Char
Module for working with Characters (Unicode code points).
Characters in Motoko represent Unicode code points in the range 0 to 0x10FFFF, excluding the surrogate code points (0xD800 through 0xDFFF).
Import from the core library to use this module.
import Char "mo:core/Char";
Some built in features not listed in this module:
- You can create a
Char
literal using single quotes, e.g. 'A', '1', '漢' - You can compare characters using
<
,<=
,==
,!=
,>=
,>
operators - You can convert a single-character
Text
to aChar
using:Char
type annotation
For example:
let char : Char = 'A';
let unicodeChar = '漢';
let digit = '7';
assert Char.isDigit(digit);
assert Char.toText(char) == "A";
Type Char
type Char = Prim.Types.Char
Characters represented as Unicode code points.
Function toNat32
func toNat32(char : Char) : Nat32
Convert character char
to a word containing its Unicode scalar value.
Example:
let char = 'A';
let unicode = Char.toNat32(char);
assert unicode == 65;
Function fromNat32
func fromNat32(nat32 : Nat32) : Char
Convert w
to a character.
Traps if w
is not a valid Unicode scalar value.
Value w
is valid if, and only if, w < 0xD800 or (0xE000 <= w and w <= 0x10FFFF)
.
Example:
let unicode : Nat32 = 65;
let char = Char.fromNat32(unicode);
assert char == 'A';
Function toText
func toText(char : Char) : Text
Convert character char
to single character text.
Example:
let char = '漢';
let text = Char.toText(char);
assert text == "漢";
Function isDigit
func isDigit(char : Char) : Bool
Returns true
when char
is a decimal digit between 0
and 9
, otherwise false
.
Example:
assert Char.isDigit('5');
assert not Char.isDigit('A');
Function isWhitespace
func isWhitespace(char : Char) : Bool
Returns whether char
is a whitespace character.
Whitespace characters include space, tab, newline, etc.
Example:
assert Char.isWhitespace(' ');
assert Char.isWhitespace('\n');
assert not Char.isWhitespace('A');
Function isLower
func isLower(char : Char) : Bool
Returns whether char
is a lowercase character.
Example:
assert Char.isLower('a');
assert not Char.isLower('A');
Function isUpper
func isUpper(char : Char) : Bool
Returns whether char
is an uppercase character.
Example:
assert Char.isUpper('A');
assert not Char.isUpper('a');
Function isAlphabetic
func isAlphabetic(char : Char) : Bool
Returns whether char
is an alphabetic character.
Example:
assert Char.isAlphabetic('A');
assert Char.isAlphabetic('漢');
assert not Char.isAlphabetic('1');
Function equal
func equal(a : Char, b : Char) : Bool
Returns a == b
.
Example:
assert Char.equal('A', 'A');
assert not Char.equal('A', 'B');
Note: The reason why this function is defined in this library (in addition
to the existing ==
operator) is so that you can use it as a function value
to pass to a higher order function.
Function notEqual
func notEqual(a : Char, b : Char) : Bool
Returns a != b
.
Example:
assert Char.notEqual('A', 'B');
assert not Char.notEqual('A', 'A');
Note: The reason why this function is defined in this library (in addition
to the existing !=
operator) is so that you can use it as a function value
to pass to a higher order function.
Function less
func less(a : Char, b : Char) : Bool
Returns a < b
.
Example:
assert Char.less('A', 'B');
assert not Char.less('B', 'A');
Note: The reason why this function is defined in this library (in addition
to the existing <
operator) is so that you can use it as a function value
to pass to a higher order function.
Function lessOrEqual
func lessOrEqual(a : Char, b : Char) : Bool
Returns a <= b
.
Example:
assert Char.lessOrEqual('A', 'A');
assert Char.lessOrEqual('A', 'B');
assert not Char.lessOrEqual('B', 'A');
Note: The reason why this function is defined in this library (in addition
to the existing <=
operator) is so that you can use it as a function value
to pass to a higher order function.
Function greater
func greater(a : Char, b : Char) : Bool
Returns a > b
.
Example:
assert Char.greater('B', 'A');
assert not Char.greater('A', 'B');
Note: The reason why this function is defined in this library (in addition
to the existing >
operator) is so that you can use it as a function value
to pass to a higher order function.
Function greaterOrEqual
func greaterOrEqual(a : Char, b : Char) : Bool
Returns a >= b
.
Example:
assert Char.greaterOrEqual('B', 'A');
assert Char.greaterOrEqual('A', 'A');
assert not Char.greaterOrEqual('A', 'B');
Note: The reason why this function is defined in this library (in addition
to the existing >=
operator) is so that you can use it as a function value
to pass to a higher order function.
Function compare
func compare(a : Char, b : Char) : {#less; #equal; #greater}
Returns the order of a
and b
.
Example:
assert Char.compare('A', 'B') == #less;
assert Char.compare('B', 'A') == #greater;
assert Char.compare('A', 'A') == #equal;