Modules · Data · Text

Text

Strings and the operations over them — measuring, comparing, searching, slicing, changing case, trimming, splitting and joining, padding, validating against character sets, formatting numbers, hashing, and URL encoding. Text values are the native Text type, which also groups these functions; every one is reactive, so a derived string recomputes whenever the values it reads change. Predicates such as Text.Contains return a Logic.Maybe, and the character-set constants pair with Text.AllowChars to sanitise input.

Contents

Types

Constants

Functions

Types

Text

A string of characters: the built-in text type, which also groups the text functions and character-set constants.

Definition

TYPE Text
  NATIVE js "string"
  NATIVE dart "String"
  NATIVE swift "String"
  NATIVE go "string"
A native string type, backed by each platform’s own string.

Example

LET Text greeting = "Hello, world"
A text value written as a string literal.

Constants

Text.Characters.Alpha

The ASCII letters, upper and lower case.

Definition

LET Alpha = "ABC…XYZabc…xyz"

Text.Characters.Alphanumeric

The ASCII letters and digits.

Definition

LET Alphanumeric = "ABC…XYZabc…xyz0123456789"

Text.Characters.DatabaseName

The characters allowed in a database name.

Definition

LET DatabaseName = "A…Za…z0…9_"

Text.Characters.Hex

The hexadecimal digits, upper and lower case.

Definition

LET Hex = "0123456789ABCDEFabcdef"

Text.Characters.HexLower

The lowercase hexadecimal digits.

Definition

LET HexLower = "0123456789abcdef"

Text.Characters.HexUpper

The uppercase hexadecimal digits.

Definition

LET HexUpper = "0123456789ABCDEF"

Text.Characters.Lower

The lowercase ASCII letters a to z.

Definition

LET Lower = "abc…xyz"

Text.Characters.Numeric

The decimal digits 0 to 9.

Definition

LET Numeric = "0123456789"

Text.Characters.Upper

The uppercase ASCII letters A to Z.

Definition

LET Upper = "ABC…XYZ"

Text.Characters.Urlname

The characters allowed in a URL-safe name.

Definition

LET Urlname = "A…Za…z0…9-._"

Functions

Text.AllowChars

Keeps only the characters in an allowed set.

Parameters

NameTypeDescription
stringTextThe string to filter.
allowedCharsTextThe characters to keep.

Returns

Returns Textstring with every character not in allowedChars removed.

Example

LET Text safe = Text.AllowChars input Text.Characters.Alphanumeric
Strips everything but letters and digits.

Text.Append

Joins two or more strings end to end.

Parameters

NameTypeDescription
aTextThe leading string.
bTextThe next string.
cCollection.Optional(Text)Third string, appended when present.
dCollection.Optional(Text)Fourth string, appended when present.
eCollection.Optional(Text)Fifth string, appended when present.
fCollection.Optional(Text)Sixth string, appended when present.
gCollection.Optional(Text)Seventh string, appended when present.

Returns

Returns Texta and b joined end to end, followed by each of c through g that holds a value, in order.

Example

LET Text url = Text.Append base path queryOptional
Appends path to base, then queryOptional when it holds a value.

Text.AppendOptional

Appends a second string only when it is present.

Parameters

NameTypeDescription
textTextThe base string.
optionalTextCollection.Optional(Text)The string to append when present.

Returns

Returns Texttext, with optionalText appended when it holds a value.

Text.CharAt

The character at a position.

Parameters

NameTypeDescription
textTextThe source string.
posMaths.IntegerZero-based index.
optionalDefaultCharCollection.Optional(Text)Returned when pos is out of range; empty when absent.

Returns

Returns Text — the character at pos, or optionalDefaultChar when out of range.

Text.Contains

Yes when a substring occurs anywhere.

Parameters

NameTypeDescription
haystackTextThe string to search.
needleTextThe substring to look for.

Returns

Returns Logic.Maybe — yes when needle occurs anywhere in haystack.

Example

LET Logic.Maybe hit = Text.Contains haystack "cat"
Yes when the text contains “cat”.

Text.ContainsOnly

Yes when every character is allowed.

Parameters

NameTypeDescription
stringTextThe string to test.
allowedCharsTextThe permitted characters.

Returns

Returns Logic.Maybe — yes when every character of string is in allowedChars.

Text.Emit

Emits a piece to the enclosing Gather.

Parameters

NameTypeDescription
pieceTextThe string piece to contribute.

Returns

Returns nothing — it adds piece to the enclosing Text.Gather.

Text.Empty

Yes when the text has no characters.

Parameters

NameTypeDescription
textTextThe string to test.

Returns

Returns Logic.Maybe — yes when text has no characters.

Example

LET Logic.Maybe blank = Text.Empty input
Yes while the input is empty.

Text.Encode.Url

Percent-encodes text for use in a URL.

Parameters

NameTypeDescription
inputTextThe string to encode.

Returns

Returns Textinput percent-encoded so it is safe inside a URL.

Example

LET Text q = Text.Encode.Url query
Percent-encodes a query string for a URL.

Text.EndsWith

Yes when the text ends with a suffix.

Parameters

NameTypeDescription
haystackTextThe string to test.
needleTextThe suffix to look for.

Returns

Returns Logic.Maybe — yes when haystack ends with needle.

Text.Equal

Yes when two strings are equal.

Parameters

NameTypeDescription
aTextFirst string.
bTextSecond string.

Returns

Returns Logic.Maybe — yes when a and b are exactly equal.

Text.FormatNumber

Formats a number with decimals and separators.

Parameters

NameTypeDescription
nMaths.RealThe number to format.
optionalDecimalPlacesCollection.Optional(Maths.Integer)Decimal places to show; 0 when absent.
optionalAddThousandsSepCollection.Optional(Logic.Maybe)Whether to group thousands; yes when absent.

Returns

Returns Textn formatted with the given decimal places and optional thousands separators.

Example

LET Text price = Text.FormatNumber amount 2 yes
Formats a value with two decimals and grouped thousands.

Text.Gather

Collects emitted pieces and joins them.

Block

The block whose Text.Emit calls contribute the pieces to join.

Returns

Returns Text — the pieces emitted within its block, concatenated in order.

Example

LET Text line = Text.Gather
Text.Emit "Hello"
Text.Emit " world"
Builds a string from the pieces emitted inside the block.

Text.GatherJoin

Collects emitted pieces and joins them with a separator.

Parameters

NameTypeDescription
separatorTextText placed between the gathered pieces.

Block

The block whose Text.Emit calls contribute the pieces to join.

Returns

Returns Text — the pieces emitted within its block, joined by separator.

Text.GetBetween

The text between two markers.

Parameters

NameTypeDescription
textTextThe string to search.
aTextThe opening marker.
bTextThe closing marker.

Returns

Returns Collection.Optional(Text) — the text between the first a and the next b, or nothing when a is absent.

Text.GetEverythingAfter

The text after the first marker.

Parameters

NameTypeDescription
strTextThe string to search.
substrTextThe marker to start after.

Returns

Returns Collection.Optional(Text) — the part of str after the first substr, or nothing when absent.

Text.GetEverythingBefore

The text before the first marker.

Parameters

NameTypeDescription
haystackTextThe string to search.
needleTextThe marker to stop at.

Returns

Returns Text — the part of haystack before the first needle.

Text.IndexOf

The position of a substring, or -1.

Parameters

NameTypeDescription
haystackTextThe string to search in.
needleTextThe substring to find.
optionalOffsetCollection.Optional(Maths.Integer)Index to start searching from; the start when absent.

Returns

Returns Maths.Integer — the index of the first needle at or after optionalOffset, or -1 when not found.

Text.Interpolate

Blends one string into another by progress.

Parameters

NameTypeDescription
startTextThe string at progress 0.
targetTextThe string at progress 1.
progressMaths.RealHow far between, 0 to 1.

Returns

Returns Text — a blend of start and target set by progress.

Example

LET Text tween = Text.Interpolate from to progress
Animates one label into another as progress runs 0 to 1.

Text.IsLower

Yes when the text is all lowercase letters.

Parameters

NameTypeDescription
textTextThe string to test.

Returns

Returns Logic.Maybe — yes when every character of text is a lowercase ASCII letter.

Text.IsWhitespace

Yes when the text is empty or only whitespace.

Parameters

NameTypeDescription
textTextThe string to test.

Returns

Returns Logic.Maybe — yes when text is empty or contains only whitespace.

Text.Join

Joins an array of strings into one.

Parameters

NameTypeDescription
elementsCollection.Array(Text)The strings to join.
optionalSeparatorCollection.Optional(Text)Text placed between elements; none when absent.

Returns

Returns Text — the elements concatenated, separated by optionalSeparator when given.

Example

LET Text csv = Text.Join parts ","
Joins the pieces with commas.

Text.Length

The number of characters in the text.

Parameters

NameTypeDescription
textTextThe string to measure.

Returns

Returns Maths.Integer — the number of characters in text.

Example

LET Maths.Integer size = Text.Length message
The length of the message, recomputed as it changes.

Text.Lower

The text in lower case.

Parameters

NameTypeDescription
textTextThe string to convert.

Returns

Returns Texttext with every letter lower-cased.

Text.Matches

Yes when the text matches a regular expression.

Parameters

NameTypeDescription
textTextThe string to test.
patternTextThe regular expression.

Returns

Returns Logic.Maybe — yes when text matches the regular expression pattern.

Text.NotEmpty

Yes when the text has at least one character.

Parameters

NameTypeDescription
textTextThe string to test.

Returns

Returns Logic.Maybe — yes when text has at least one character.

Text.NotEqual

Yes when two strings differ.

Parameters

NameTypeDescription
aTextFirst string.
bTextSecond string.

Returns

Returns Logic.Maybe — yes when a and b are not equal.

Text.Option

Runs its block when its value matches the enclosing Select.

Parameters

NameTypeDescription
optionValueTextThe value this option matches.

Block

The content to run when optionValue equals the value selected by the enclosing Text.Select.

Returns

Returns nothing — it runs its block only on a match.

Text.PadLeft

Pads a string on the left to a length.

Parameters

NameTypeDescription
stringTextThe string to pad.
lengthMaths.IntegerThe target length.
optionalPadStringCollection.Optional(Text)The padding text; a space when absent.

Returns

Returns Textstring padded on the left to length with optionalPadString.

Example

LET Text code = Text.PadLeft digits 4 "0"
Zero-pads a number to four digits.

Text.RemoveChars

Removes characters from the start or end.

Parameters

NameTypeDescription
textTextThe source string.
numMaths.IntegerCharacters to drop from the start, or from the end when negative.

Returns

Returns Texttext with num characters removed from the start, or from the end when negative.

Text.RemoveFromEnd

Removes a suffix if present.

Parameters

NameTypeDescription
haystackTextThe source string.
needleTextThe suffix to remove.

Returns

Returns Texthaystack with a trailing needle removed when present.

Text.RemoveFromStart

Removes a prefix if present.

Parameters

NameTypeDescription
haystackTextThe source string.
needleTextThe prefix to remove.

Returns

Returns Texthaystack with a leading needle removed when present.

Text.Replace

Replaces the first occurrence of a substring.

Parameters

NameTypeDescription
searchTextThe substring to find.
replaceTextThe replacement.
haystackTextThe string to search in.

Returns

Returns Texthaystack with the first search replaced by replace.

Text.ReplaceAll

Replaces every occurrence of a substring.

Parameters

NameTypeDescription
searchTextThe substring to find.
replaceTextThe replacement.
haystackTextThe string to search in.

Returns

Returns Texthaystack with every search replaced by replace.

Text.Select

Opens a group that runs the matching Option for a string value.

Parameters

NameTypeDescription
valueTextThe string the options are matched against.

Block

An indented block of Text.Option entries; the one whose value equals value runs.

Returns

Returns nothing — it publishes value to the enclosed options over its block.

Example

Text.Select role
Text.Option "admin"
  Ui.Content.Text "Welcome, admin"
Text.Option "guest"
  Ui.Content.Text "Please sign in"
Runs the block of whichever option matches role.

Text.Sha1

A hexadecimal digest of the text.

Parameters

NameTypeDescription
inputTextThe string to digest.

Returns

Returns Text — a hexadecimal digest string derived from input.

Text.Split

Divides a string into an array on a delimiter.

Parameters

NameTypeDescription
stringTextThe string to split.
delimiterTextThe separator to split on.

Returns

Returns Collection.Array(Text) — the pieces of string divided at each delimiter.

Example

LET Collection.Array(Text) parts = Text.Split line ","
Splits a comma-separated line into its fields.

Text.Start

The first characters of a string.

Parameters

NameTypeDescription
textTextThe source string.
lengthMaths.IntegerHow many leading characters to take.

Returns

Returns Text — the first length characters of text.

Text.StartsWith

Yes when the text begins with a prefix.

Parameters

NameTypeDescription
textTextThe string to test.
startsWithTextThe prefix to look for.

Returns

Returns Logic.Maybe — yes when text begins with startsWith.

Text.Substring

Extracts part of a string by position and length.

Parameters

NameTypeDescription
stringTextThe source string.
startMaths.IntegerZero-based start index.
optionalLengthCollection.Optional(Maths.Integer)How many characters to take; to the end when absent.

Returns

Returns Text — the slice of string from start, up to optionalLength characters.

Example

LET Text head = Text.Substring text 0 5
The first five characters of the text.

Text.Trim

Removes whitespace from both ends.

Parameters

NameTypeDescription
textTextThe string to trim.

Returns

Returns Texttext with leading and trailing whitespace removed.

Text.TrimEnd

Removes trailing whitespace.

Parameters

NameTypeDescription
textTextThe string to trim.

Returns

Returns Texttext with trailing whitespace removed.

Text.TrimStart

Removes leading whitespace.

Parameters

NameTypeDescription
textTextThe string to trim.

Returns

Returns Texttext with leading whitespace removed.

Text.Upper

The text in upper case.

Parameters

NameTypeDescription
textTextThe string to convert.

Returns

Returns Texttext with every letter upper-cased.

Example

LET Text shout = Text.Upper name
Uppercases a name reactively.

Text.UpperFirst

Capitalises the first letter of each word.

Parameters

NameTypeDescription
textTextThe string to convert.

Returns

Returns Texttext with each word's first letter capitalised and the rest lower-cased.