Text

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

import Text "mo:base/Text";

Types

Type Pattern

Analysis

Function size
Function contains
Function startsWith
Function endsWith
Function stripStart
Function stripEnd
Function trimStart
Function trimEnd
Function trim

Conversion

Function fromChar
Function toIter
Function fromIter
Function hash
Function encodeUtf8
Function decodeUtf8

Comparison

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

Transformation

Function replace
Function concat
Function join
Function map
Function translate
Function split
Function tokens
Function compareWith

Type Pattern

The Pattern type is a useful variant searching through Text values. If we traverse (visit each character of) a Text, we might look for a text pattern that matches a Pattern.

This could be a character in case of #char : Char or a string in case of a #text : Text. We could also provide a function of type Char -> Bool that looks at each character and performs some logic to test whether some predicate is true or false about that character.

type Pattern = {
    #char : Char;
    #text : Text;
    #predicate : (Char -> Bool);
};

Text.size

func size(t : Text) : Nat

The function size takes one Text value as a argument and returns a Nat value.

import Text "mo:base/Text";

let text : Text = "blockchain";

Text.size(text)

Text.contains

func contains(t : Text, p : Pattern) : Bool
import Text "mo:base/Text";

let text : Text = "blockchain";
let letter : Text.Pattern = #char 'k';

Text.contains(text, letter);

Text.startsWith

func startsWith(t : Text, p : Pattern) : Bool
import Text "mo:base/Text";

let text : Text = "blockchain";
let letter : Text.Pattern = #text "block";

Text.startsWith(text, letter);

Text.endsWith

func endsWith(t : Text, p : Pattern) : Bool
import Text "mo:base/Text";
import Char "mo:base/Char";

let text : Text = "blockchain";
let letter : Text.Pattern = #predicate(func c = Char.isAlphabetic(c));

Text.endsWith(text, letter);

Text.stripStart

func stripStart(t : Text, p : Pattern) : ?Text
import Text "mo:base/Text";

let text : Text = "@blockchain";
let letter : Text.Pattern = #char '@';

Text.stripStart(text, letter);

Text.stripEnd

func stripEnd(t : Text, p : Pattern) : ?Text
import Text "mo:base/Text";

let text : Text = "blockchain&";
let letter : Text.Pattern = #char '&';

Text.stripEnd(text, letter);

Text.trimStart

func trimStart(t : Text, p : Pattern) : Text
import Text "mo:base/Text";

let text : Text = "vvvblockchain";
let letter : Text.Pattern = #char 'v';

Text.trimStart(text, letter);

Text.trimEnd

func trimEnd(t : Text, p : Pattern) : Text
import Text "mo:base/Text";

let text : Text = "blockchain****";
let letter : Text.Pattern = #char '*';

Text.trimEnd(text, letter);

Text.trim

func trim(t : Text, p : Pattern) : Text
import Text "mo:base/Text";

let text : Text = "@@blockchain@@";
let letter : Text.Pattern = #char '@';

Text.trim(text, letter);

Text.fromChar

func fromChar : (c : Char) -> Text

The function fromChar takes one Textvalue as a argument and returns a Char value.

import Text "mo:base/Text";

let character : Char = 'c';

Text.fromChar(character)

Text.toIter

func toIter(t : Text) : Iter.Iter<Char>
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let text : Text = "xyz";

let iter : Iter.Iter<Char> = Text.toIter(text);

Iter.toArray(iter)

Text.fromIter

func fromIter(cs : Iter.Iter<Char>) : Text
import Text "mo:base/Text";
import Array "mo:base/Array";
import Iter "mo:base/Iter";

let array : [Char] = ['I', 'C', 'P'];

let iter : Iter.Iter<Char> = Array.vals(array);

Text.fromIter(iter)

Text.hash

func hash(t : Text) : Hash.Hash

The function hash takes one Text value as a argument and returns a Hash value.

import Text "mo:base/Text";

let text : Text = "xyz";

Text.hash(text)

Text.encodeUtf8

func encodeUtf8 : Text -> Blob

The function encodeUtf8 takes one Text value as a argument and returns a Bool value.

import Blob "mo:base/Blob";
import Text "mo:base/Text";

let t : Text = "ICP";

Text.encodeUtf8(t);

Text.decodeUtf8

func decodeUtf8 : Blob -> ?Text

The function decodeUtf8 takes one Blob value as a argument and returns a ?Text value. If the blob is not valid UTF8, then this function returns null.

import Blob "mo:base/Blob";
import Text "mo:base/Text";

let array : [Nat8] = [73, 67, 80];
let blob : Blob = Blob.fromArray(array);

Text.decodeUtf8(blob);

Text.equal

func equal(t1 : Text, t2 : Text) : Bool

The function equal takes two Text value as a argument and returns a Bool value.

import Text "mo:base/Text";

let a : Text = "text_A";
let b : Text = "text_A";

Text.equal(a, b);

Text.notEqual

func notEqual(t1 : Text, t2 : Text) : Bool

The function notEqual takes two Text value as a argument and returns a Bool value.

import Text "mo:base/Text";

let a : Text = "text_B";
let b : Text = "text_A";

Text.notEqual(a, b);

Text.less

func less(t1 : Text, t2 : Text) : Bool

The function less takes two Text value as a argument and returns a Bool value.

import Text "mo:base/Text";

let a : Text = "text_A";
let b : Text = "text_B";

Text.less(a, b);

Text.lessOrEqual

func lessOrEqual(t1 : Text, t2 : Text) : Bool

The function lessOrEqual takes two Text value as a argument and returns a Bool value.

import Text "mo:base/Text";

let a : Text = "text_A";
let b : Text = "text_B";

Text.lessOrEqual(a, b);

Text.greater

func greater(t1 : Text, t2 : Text) : Bool

The function greater takes two Text value as a argument and returns a Bool value.

import Text "mo:base/Text";

let a : Text = "text_B";
let b : Text = "text_A";

Text.greater(a, b);

Text.greaterOrEqual

func greaterOrEqual(t1 : Text, t2 : Text) : Bool

The function greaterOrEqual takes two Text value as a argument and returns a Bool value.

import Text "mo:base/Text";

let a : Text = "text_B";
let b : Text = "text_B";

Text.greaterOrEqual(a, b);

Text.compare

func compare(t1 : Text, t2 : Text) : {#less; #equal; #greater}

The function compare takes two Text value as a argument and returns an Order value.

import Text "mo:base/Text";

let a : Text = "text_A";
let b : Text = "text_B";

Text.compare(a, b);

Text.replace

func replace(t : Text, p : Pattern, r : Text) : Text
Parameters
Variable argument1t : Text
Variable argument2r : Text
Object argumentp : pattern
Return typeIter.Iter<Text>
import Text "mo:base/Text";

let text : Text = "blockchain";
let letter : Text.Pattern = #char 'b';

Text.replace(text, letter, "c");

Text.concat

func concat(t1 : Text, t2 : Text) : Text

The function concat takes two Text value as a arguments and returns a Text value. It is equivalent to the # operator.

import Text "mo:base/Text";

let t1 : Text = "Internet";
let t2 : Text = "Computer";

Text.concat(t1, t2);

Text.join

func join(sep : Text, ts : Iter.Iter<Text>) : Text
Parameters
Variable argumentsep : Text
Object argumentts : Iter.Iter<Text>
Return typeText
import Text "mo:base/Text";
import Array "mo:base/Array";
import Iter "mo:base/Iter";

let array : [Text] = ["Internet", "Computer", "Protocol"];

let iter : Iter.Iter<Text> = Array.vals(array);

let text : Text = "-";

Text.join(text, iter)

Text.map

func map(t : Text, f : Char -> Char) : Text
Parameters
Variable argumentt : Text
Function argumentf : Char -> Char
Return typeText
import Text "mo:base/Text";

let text : Text = "PCI";

func change(c : Char) : Char {
  if (c == 'P') { return 'I' } else if (c == 'I') { return 'P' } else {
    return 'C';
  };
};

Text.map(text, change)

Text.translate

func translate(t : Text, f : Char -> Text) : Text
Parameters
Variable argumentt : Text
Function argumentf : Char -> Char
Return typeText
import Text "mo:base/Text";

let text : Text = "*ICP*";

func change(c : Char) : Text {
  if (c == '*') { return "!" } else { return Text.fromChar(c) };
};

Text.translate(text, change)

Text.split

func split(t : Text, p : Pattern) : Iter.Iter<Text>
Parameters
Variable argumentt : Text
Object argumentp : pattern
Return typeIter.Iter<Text>
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let text : Text = "this is internet computer";

let letter : Text.Pattern = #char ' ';

let split : Iter.Iter<Text> = Text.split(text, letter);

Iter.toArray(split)

Text.tokens

func tokens(t : Text, p : Pattern) : Iter.Iter<Text>
Parameters
Variable argumentt : Text
Object argumentp : pattern
Return typeIter.Iter<Text>
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let text : Text = "abcabcabc";

let letter : Text.Pattern = #char 'a';

let token : Iter.Iter<Text> = Text.tokens(text, letter);

Iter.toArray(token)

Text.compareWith

func compareWith(t1 : Text, t2 : Text, cmp : (Char, Char) -> {#less; #equal; #greater}) : {#less; #equal; #greater}
Parameters
Variable argument1t1 : Text
Variable argument2t2 : Text
Function argumentcmp : (Char, Char) -> {#less; #equal; #greater}
Return type{#less; #equal; #greater}
import Text "mo:base/Text";
import Order "mo:base/Order";
import Char "mo:base/Char";

let text1 : Text = "icp";
let text2 : Text = "ICP";

func compare(c1 : Char, c2 : Char) : Order.Order {
    Char.compare(c1, c2);
};

Text.compareWith(text1, text2, compare);