Skip to content

SpecificationXII · Library

12.2

Text functions

Text functions measure, trim, search and transform strings without ever modifying the original: they always return a new one.

Markdown version

12.2.1 How it works

They all work by code points: every Unicode character counts as one, accented or not. Positions start at zero.

FunctionWhat it returns
length(t)how many characters it has
substring(t, from, to)the piece between two positions, both included
trim(t)without spaces at the start or end
toUpper(t), toLower(t)with every letter changed
contains, startsWith, endsWitha Boolean
indexOf(t, part)Something(position) or Nothing
replace(t, find, put)with every occurrence changed
split(t, separator), join(v, separator)a vector of pieces, or the joined text
charAt, charCode, charFromCodea character or its Unicode code
padLeft, padRightthe text filled up to a width
Figure 12.2-1.Positions start at zero and substring includes both positions it receives: substring(t, 0, 3) is the first four characters.

The text Hola, mundo in cells numbered from 0 to 10. The first four, from 0 to 3, are highlighted as the result of substring(t, 0, 3).

ProgramA tour of the text functions

Algorithm Textos
  Define frase As String <- "  Hola, mundo  ";
  Define limpia As String <- trim(frase);
  Write "[", limpia, "] ", length(limpia);
  Write substring(limpia, 0, 3), " ", toUpper(limpia), " ", toLower("ÁRBOL");
  Write contains(limpia, "mundo"), " ", startsWith(limpia, "Ho"), " ", endsWith(limpia, "x");
  Write replace(limpia, "o", "0"), " ", charAt(limpia, 1), " ", charCode('A'), " ", charFromCode(97);
  Write padLeft("7", 3, '0'), " ", padRight("ab", 4, '.'), "|";
  Write join(split("a,b,c", ","), " - ");
EndAlgorithm

Output

[Hola, mundo] 11
Hola HOLA, MUNDO árbol
true true false
H0la, mund0 o 65 a
007 ab..|
a - b - c

The program finishes normally.

In short

  • Text functions return new strings.
  • They count Unicode characters and number from zero.
  • substring includes both positions.