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 package to use this module.
motoko name=import
import Char "mo:core/Char";
Some built in features not listed in this module:
Char literal using single quotes, e.g. 'A', '1', '漢'<, <=, ==, !=, >=, > operatorsText to a Char using :Char type annotationFor example:
motoko include=import
let char : Char = 'A';
let unicodeChar = '漢';
let digit = '7';
assert Char.isDigit(digit);
assert Char.toText(char) == "A";Characters represented as Unicode code points.
public func toNat32(self : Char) : Nat32Convert character char to a word containing its Unicode scalar value.
Example:
motoko include=import
let char = 'A';
let unicode = Char.toNat32(char);
assert unicode == 65;public func fromNat32(nat32 : Nat32) : CharConvert 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:
motoko include=import
let unicode : Nat32 = 65;
let char = Char.fromNat32(unicode);
assert char == 'A';public func toText(self : Char) : TextConvert character char to single character text.
Example:
motoko include=import
let char = '漢';
let text = Char.toText(char);
assert text == "漢";public func isDigit(self : Char) : BoolReturns true when char is a decimal digit between 0 and 9, otherwise false.
Example:
motoko include=import
assert Char.isDigit('5');
assert not Char.isDigit('A');public func isWhitespace(self : Char) : BoolReturns whether char is a whitespace character.
Whitespace characters include space, tab, newline, etc.
Example:
motoko include=import
assert Char.isWhitespace(' ');
assert Char.isWhitespace('\n');
assert not Char.isWhitespace('A');public func isLower(self : Char) : BoolReturns whether char is a lowercase character.
Example:
motoko include=import
assert Char.isLower('a');
assert not Char.isLower('A');public func isUpper(self : Char) : BoolReturns whether char is an uppercase character.
Example:
motoko include=import
assert Char.isUpper('A');
assert not Char.isUpper('a');public func isAlphabetic(self : Char) : BoolReturns whether char is an alphabetic character.
Example:
motoko include=import
assert Char.isAlphabetic('A');
assert Char.isAlphabetic('漢');
assert not Char.isAlphabetic('1');public func equal(self : Char, other : Char) : BoolReturns a == b.
Example:
motoko include=import
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.
public func notEqual(self : Char, other : Char) : BoolReturns a != b.
Example:
motoko include=import
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.
public func less(self : Char, other : Char) : BoolReturns a < b.
Example:
motoko include=import
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.
public func lessOrEqual(self : Char, other : Char) : BoolReturns a <= b.
Example:
motoko include=import
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.
public func greater(self : Char, other : Char) : BoolReturns a > b.
Example:
motoko include=import
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.
public func greaterOrEqual(self : Char, other : Char) : BoolReturns a >= b.
Example:
motoko include=import
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.