Skip to content

SpecificationIV · Expressions and operators

4.6

Concatenation

+ between texts glues them one after the other: "Hola, " + "Ana" is "Hola, Ana".

Markdown version

4.6.1 How it works

The same + sign that adds numbers concatenates texts. It accepts any combination of String and Character, and the result is always a String: even two single characters form a string of length 2.

Figure 4.6-1.Each piece is copied after the previous one, from left to right, and forms a new string.

Three boxes, "Hola, ", "Ana" and '!', with arrows converging on a larger box with "Hola, Ana!".

ProgramGluing texts

Algorithm Saludo
  Define nombre As String <- "Ana";
  Write "Hola, " + nombre + '!';
  Write 'a' + 'b', " ", length('a' + 'b');
EndAlgorithm

Output

Hola, Ana!
ab 2

The program finishes normally.

Concatenation does not convert numbers or booleans to text. To put a number in a message there are two ways: convert it with ConvertirATexto, or separate the pieces with commas in Write.

ProgramA number inside a text

Algorithm ConNumero
  Define edad As Integer <- 12;
  Write "Tienes " + toText(edad) + " años";
  Write "Tienes ", edad, " años";
EndAlgorithm

Output

Tienes 12 años
Tienes 12 años

The program finishes normally.

In short

  • + between texts or characters concatenates them and gives a String.
  • It does not convert numbers: use ConvertirATexto or commas in Write.
  • It has the same precedence as addition.