Char

In Motoko, a character literal is a single character enclosed in single quotes and has type Char. (As opposed to text literals of type Text, which may be multiple characters enclosed in double quotes.)

let char : Char = 'a';

let text : Text = "a";

The convention is to name the module alias after the file name it is defined in:

import Char "mo:base/Char";

Conversion

Function toNat32
Function fromNat32
Function toText

Utility Function

Function isDigit
Function isWhitespace
Function isLowercase
Function isUppercase
Function isAlphabetic

Comparison

Function equal
Function notEqual
Function less
Function lessOrEqual
Function greater
Function greaterOrEqual
Function compare

Char.toNat32

func toNat32 : (c : Char) -> Nat32

The function toNat32 takes one Char value and returns a Nat32 value.

import Char "mo:base/Char";

Char.toNat32('y');

Char.fromNat32

func fromNat32 : (w : Nat32) -> Char

The function fromNat32 takes one Nat32 value and returns a Char value.

import Char "mo:base/Char";

Char.fromNat32(100 : Nat32);

Char.toText

func toText : (c : Char) -> Text

The function toText takes one Char value and returns a Text value.

import Char "mo:base/Char";

Char.toText('C')

Char.isDigit

func isDigit(c : Char) : Bool

The function isDigit takes one Char value and returns a Bool value.

import Char "mo:base/Char";

Char.isDigit('5');

Char.isWhitespace

let isWhitespace : (c : Char) -> Bool

The function isWhitespace takes one Char value and returns a Bool value.

import Char "mo:base/Char";

Char.isWhitespace(' ');

Char.isLowercase

func isLowercase(c : Char) : Bool

The function isLowercase takes one Char value and returns a Bool value.

import Char "mo:base/Char";

Char.isLowercase('a');

Char.isUppercase

func isUppercase(c : Char) : Bool

The function isUppercase takes one Char value and returns a Bool value.

import Char "mo:base/Char";

Char.isUppercase('a');

Char.isAlphabetic

func isAlphabetic : (c : Char) -> Bool

The function isAlphabetic takes one Char value and returns a Bool value.

import Char "mo:base/Char";

Char.isAlphabetic('y');

Char.equal

func equal(x : Char, y : Char) : Bool

The function equal takes two Char value and returns a Bool value.

import Char "mo:base/Char";

let char1 : Char = 'a';
let char2 : Char = 'b';

Char.equal(char1, char2);

Char.notEqual

func notEqual(x : Char, y : Char) : Bool

The function notEqual takes two Char value and returns a Bool value.

import Char "mo:base/Char";

let char1 : Char = 'a';
let char2 : Char = 'b';

Char.notEqual(char1, char2);

Char.less

func less(x : Char, y : Char) : Bool

The function less takes two Char value and returns a Bool value.

import Char "mo:base/Char";

let char1 : Char = 'a';
let char2 : Char = 'b';

Char.less(char1, char2);

Char.lessOrEqual

func lessOrEqual(x : Char, y : Char) : Bool

The function lessOrEqual takes two Char value and returns a Bool value.

import Char "mo:base/Char";

let char1 : Char = 'a';
let char2 : Char = 'b';

Char.lessOrEqual(char1, char2);

Char.greater

func greater(x : Char, y : Char) : Bool

The function greater takes two Char value and returns a Bool value.

import Char "mo:base/Char";

let char1 : Char = 'b';
let char2 : Char = 'a';

Char.greater(char1, char2);

Char.greaterOrEqual

func greaterOrEqual(x : Char, y : Char) : Bool

The function greaterOrEqual takes two Char value and returns a Bool value.

import Char "mo:base/Char";

let char1 : Char = 'b';
let char2 : Char = 'a';

Char.greaterOrEqual(char1, char2);

Char.compare

func compare(x : Char, y : Char) : {#less; #equal; #greater}

The function compare takes two Char value and returns an Order value.

import Char "mo:base/Char";

let char1 : Char = 'a';
let char2 : Char = 'b';

Char.compare(char1, char2);