ProtoLang.net

Reference: Concatenate

☰ Hide Sidebar
TOKEN CONCATENATE
ALIAS ..

The Concatenate General Operator is used to combine multiple values into a single String. Unlike Arithmetic Operators which only work with numeric types, the Concatenate operator can combine any Data Type - including Booleans, Integers, Floats, and Strings - into a single string value.

Syntax

The short syntax structure uses the .. symbol:

PRINT VALUE .. VALUE

The long syntax structure uses the word CONCATENATE:

PRINT VALUE CONCATENATE VALUE

Basic String Concatenation

The most common use of Concatenate is to combine multiple strings into one.

Example: Combining String Literals

NEW STR greeting = "Hello" .. " " .. "World"
PRINTLN greeting // Displays "Hello World"

Example: Combining String Variables

NEW STR firstName = "John"
NEW STR lastName = "Doe"
NEW STR fullName = firstName .. " " .. lastName
PRINTLN fullName // Displays "John Doe"

Example: Building Sentences

NEW STR subject = "The cat"
NEW STR verb = "sat"
NEW STR object = "on the mat"
NEW STR sentence = subject .. " " .. verb .. " " .. object .. "."
PRINTLN sentence // Displays "The cat sat on the mat."

Concatenating Different Data Types

The Concatenate operator automatically converts non-string values to strings when combining them.

Example: Concatenating Strings and Integers

NEW INT age = 25
NEW STR message = "I am " .. age .. " years old"
PRINTLN message // Displays "I am 25 years old"

Example: Concatenating Strings and Floats

NEW FLOAT price = 19.99
NEW STR label = "Price: $" .. price
PRINTLN label // Displays "Price: $19.99"

Example: Concatenating Strings and Booleans

NEW BOOL isActive = TRUE
NEW STR status = "Active: " .. isActive
PRINTLN status // Displays "Active: TRUE"

Example: Mixing Multiple Data Types

NEW STR name = "Alice"
NEW INT age = 30
NEW FLOAT height = 5.6
NEW BOOL isStudent = FALSE
NEW STR info = name .. " is " .. age .. " years old, " .. height .. " feet tall. Student: " .. isStudent
PRINTLN info // Displays "Alice is 30 years old, 5.6 feet tall. Student: FALSE"

Concatenating Expressions

You can concatenate the results of Expressions directly.

Example: Concatenating Arithmetic Results

NEW INT x = 10
NEW INT y = 5
NEW STR result = "Sum: " .. ( x + y ) .. ", Product: " .. ( x * y )
PRINTLN result // Displays "Sum: 15, Product: 50"

Example: Concatenating Comparison Results

NEW INT a = 5
NEW INT b = 10
NEW STR comparison = a .. " < " .. b .. " is " .. ( a < b )
PRINTLN comparison // Displays "5 < 10 is TRUE"

Using Concatenate in Print Statements

Concatenate is commonly used directly in Print and PrintLn commands.

Example: Direct Output Concatenation

NEW INT score = 95
PRINTLN "Your score is: " .. score // Displays "Your score is: 95"

Example: Building Dynamic Messages

NEW STR username = "Alice"
NEW INT points = 1000
PRINTLN "Welcome back, " .. username .. "! You have " .. points .. " points."
// Displays "Welcome back, Alice! You have 1000 points."

Multiple Concatenations

You can chain multiple Concatenate operators to combine many values.

Example: Chaining Concatenations

NEW STR a = "A"
NEW STR b = "B"
NEW STR c = "C"
NEW STR d = "D"
NEW STR alphabet = a .. b .. c .. d
PRINTLN alphabet // Displays "ABCD"

Example: Building Complex Strings

NEW STR day = "Monday"
NEW STR month = "January"
NEW INT date = 22
NEW INT year = 2026
NEW STR fullDate = day .. ", " .. month .. " " .. date .. ", " .. year
PRINTLN fullDate // Displays "Monday, January 22, 2026"

Concatenate vs Plus

It's important to distinguish between Concatenate (..) for strings and Plus (+) for arithmetic.

Example: Concatenate Creates String, Plus Adds Numbers

NEW INT x = 5
NEW INT y = 3
NEW INT sum = x + y // Plus operator: adds numbers
NEW STR combined = x .. y // Concatenate operator: creates string
PRINTLN sum // Displays "8"
PRINTLN combined // Displays "53"

Practical Applications

Example: Creating Labels

NEW INT counter = 1
WHILE counter <= 5
    PRINTLN "Item " .. counter
    SET counter = counter + 1
END WHILE
// Displays:
// Item 1
// Item 2
// Item 3
// Item 4
// Item 5

Example: Building Reports

NEW STR productName = "Widget"
NEW FLOAT quantity = 100.0
NEW FLOAT price = 9.99
NEW FLOAT total = quantity * price
PRINTLN "Product: " .. productName
PRINTLN "Quantity: " .. quantity
PRINTLN "Price: $" .. price
PRINTLN "Total: $" .. total

Important Notes

  • Concatenate (..) works with all data types, converting them to strings automatically.
  • Use Concatenate (..) for strings; use Plus (+) for arithmetic.
  • Multiple values can be chained with multiple Concatenate operators.
  • Non-string values are automatically converted to their string representation.
  • The result of a Concatenate operation is always a String.

Common Errors

Error: Using Plus Instead of Concatenate for Strings

NEW STR greeting = "Hello" + "World" // Type error! Use .. for strings, not +
NEW STR greeting = "Hello" .. "World" // Correct! Concatenate operator works with strings

Error: Expecting Arithmetic with Concatenate

NEW INT result = 5 .. 3 // Type error! .. produces String, not Integer
NEW INT result = 5 + 3 // Correct! + performs arithmetic
NEW STR combined = 5 .. 3 // Correct! .. creates string "53"

Error: Forgetting Spaces in Concatenation

NEW STR fullName = "John" .. "Doe"
PRINTLN fullName // Displays "JohnDoe" (no space!)
NEW STR fullName = "John" .. " " .. "Doe"
PRINTLN fullName // Displays "John Doe" (with space)