String Concatenation Operators In Different Programming Languages

Horja Robert Emanuel
Image generated with Midjourney (AI)

How many different concatenation operators do you know? How many different programming languages do you use in your professional work? How many different programming concepts are you familiar with? Last but not least: how many programming concepts do you want to learn?

Too many questions? Let’s dive into some well-documented answers…

I want to start with the last question: if you’d like to learn everything, be aware that it’s almost impossible. Why? Because your time is limited and also is your perspective.

So, what should you do? First, focus on learning those things that are important and necessary right now, then learn other things that are also important for your career.

For example, in this blog post you’ll learn about string concatenation operators in seven programming languages. You don’t need to learn all these programming languages in one month (or even a year), but you should be aware of their existence and have the desire to learn them in the future.

A.I. will grow faster and faster, but if you can do your job in multiple programming language, plus you know several programming concepts and you are a mastermind in software architecture, you’ll always have a great job.

That being said (or written), I think it’s wise to go forward with small steps (one step at a time). Today you’ll discover seven different string concatenation operators.

I will list them one by one, then we’ll go into details together:

  • PHP . (dot symbol)
  • Lua .. (dot-dot symbol)
  • Java + (plus symbol)
  • Erlang ++ (plus-plus symbol)
  • Elixir <> (“less than” and “greater than” symbols)
  • Ruby << (“less-less than” symbol or “double-left arrow” symbol)
  • Visual Basic & (ampersand symbol)

Let’s go!


Concatenation Operator In PHP

First of all, let’s define what concatenation means.

Generally, concatenation means linking things together in a series or chain.

In a natural language, concatenation can be done by the space between words or with a comma (,), or semicolon (;). This sentence or paragraph has a lot of words linked together; it’s natural to use a space between words in a sentence or to use a comma when you enumerate something.

The best example that comes to my mind right now for concatenation is when you put a trailer to a car. This is a clear example of what concatenation is in a real world.

A simpler example, a trivial one, is when you put the links together in a chain or when you assemble them. Does it make sense to you? Great!

In programming languages, concatenation refers to joining one or more strings (one behind the other). You’ll see this term mostly when it comes to strings.

This article was born when I realized that PHP has the dot or point (.) as a symbol for string concatenation and my favorite programming language, Java, has the plus symbol (+). I also used to play around a lot with C, C++, C#, JavaScript and Python, and they all have the plus symbol for string concatenation. Why PHP is different?

Then I found out that there are other operators for different programming languages and I decided to write this blog post.

Therefore, here is an example:

$one = "Hello, ";
$two = "World!";
$three = $one . $two;
echo $three;
// Output: "Hello, World!"

Another example good to know is when we use the concatenation operator together with the assignment operator. How do we do that?

I’ll give you a simple example with the plus (+) operator. Let’s say that $x = 1 and we want to make $x = $x + 10. Instead of doing that, we could write this statement: $x += 10. It’s the same? Yes it is!

Now, look at this one:

$add = "add";
$add .= "ing";
echo $add;
// The $add variable will have the following string: "adding"

What do you think about this combination of the concatenation operator and the assignment operator? I like this trick!

It’s time to add another dot (..) and to take a look at the concatenation operator in Lua. Have you ever written code in Lua?


Concatenation Operator In Lua

The concatenation operator in Lua is dot-dot (..). Pretty easy, right?

Well, I think you deserve more information – properly done, like a well-cooked meal.

Lua means “moon” in Portuguese and it was developed by Roberto Ierusalimschy, Luiz Henrique de Figueiredo and Waldemer Celes at the Pontifical Catholic University of Rio de Janeiro, in Brazil.

It’s important to know that Lua is a scripting language (like Python), and was initially designed to extend applications written in C programming language.

If you ever want to learn Lua at a high level, you should know that Lua doesn’t have a classic OOP style, but you can use OOP principles with metatables and metamethods. Do you want to be the best? May God bless you to become the best!

Now let’s take a look at an example with Lua concatenation operator. I’ll use dot-dot (..) to concatenate two string variables:

local one = "add"
local two = "ing"
local three = one .. two
print(three)
-- The 'three' string variable will output "adding"

Notice that I declared every variable using the “local” keyword. Otherwise, the variables were global and it’s not a good practice to use global variables in Lua.

Another important thing is that I don’t put a semicolon at the end of a statement. Why? I could have put a semicolon, but is not necessary.

Do you want another example of the concatenation operator in Lua? The key idea is that every concatenation operator links strings together. Here’s a simple “train” example:

local a = "nil " 
local b = "boolean "
local c = "number "
local d = "string "
local e = "function "
local f = "userdata "
local g = "thread "
local h = "table"
local lua_data_types = a .. b .. c .. d .. e .. f .. g .. h
print(lua_data_types)
--[[
It will output all Lua data types: 
nil boolean number string function userdata thread table
]]

In Lua, dash-dash (–) is using for starting a single-line comment, two times dash-dash open square [[ for opening the multi-line comment and two times dash-dash closing square ]] for closing the multi-line comment.

Lua is popular for its utility in gaming (e.g. Roblox, Angry Birds, World of Warcraft) and in notable software applications such as Nginx, Redis and Wireshark.


Concatenation Operator In Java

Java is one of my favorite programming language. Which one is yours?

In Java, the concatenation operator is the + symbol, commonly used for mathematical operations.

A synonym for concatenation is joining or combining.

There are some interesting things worth knowing about the concatenation operator in Java. Let’s take a look at a basic example:

String firstString = "Hello, ";
String secondString = "World";
System.out.println(firstString + secondString);
// Output: Hello, World!

In the previous example, I defined two variables (notice the space after the value of the first variable?), then, on the third line, the output will be the sum of those two variables, but that sum is between two strings, and the result will be a combined string.

We can use concatenation between strings and numbers. How? Watch this and also the results from the comments line:

System.out.println(10 + "20" + 30);
// Output: 102030
System.out.println(10 + 20 + "30");
/* 
Output: 3030
First, the program will add 10 to 20,
then that result will be concatenated
with the "30" string
*/

If the first two operands are numbers, the plus (+) operator will behave as a mathematical one. Every time when the program encounters a string, the result will be a string due to concatenation.

I recommend you to learn Java! It’s a powerful programming language!

Java’s official motto is “Write Once, Run Anywhere” (WORA), which means that any program can been compiled on different operating system.

Did you know that the name of JavaScript was chosen as part of a marketing strategy to capitalize on the rising popularity of Java at that time? Nowadays, JavaScript is extremely powerful. I don’t want to compare Java with JavaScript because each of them is very useful in certain areas.

The concatenation operator in Java, the plus (+) operator, is a well-known concatenation operator in other programming languages like C, C++, C#, Go, JavaScript, Kotlin, and Python.

In the world of programming languages, we also have the ++ operator for concatenation. In which programming language is it used? The main programming language that uses the ++ operator for string concatenation is Erlang.


Concatenation Operator In Erlang

Erlang is a programming language developed by Ericsson, specifically designed for distributed systems. Did you know that WhatsApp used Erlang for its backend infrastructure?

The concatenation operator in Erlang are the ++ (plus-plus) symbols. For me, it’s very interesting to know that because I’ve never met before that combination. Have you met? Do you know how to code in Erlang?

As a brief piece of advice, if you want to become a great software architect, you absolutely need to master distributed systems, and that goes hand in hand with mastering a programming language like Erlang, Elixir, Go, or Rust.

That being said, let’s see a specific example with the concatenation operator in Erlang:

io:fwrite("Hello, " ++ "World~n"),
io:fwrite("One, " ++ "Two, " ++ "Three").
%%
The output will be:
Hello, World
One, Two, Three
%%

Don’t worry if you don’t know the Erlang’s syntax! In Erlang, every statement must end with the dot symbol (.) and a multi-line comment starts and ends with double percentage symbol (%%). One important thing to notice is “~n” which is the equivalent for “\n”, representing a new line.

Let’s write a simple example, just to highlight the ++ operator:

"This " ++ "example " ++ "is " ++ "great!".
% Output: This example is great.

How do you like it? I would like to learn Erlang in the future! I hope you do too! The world needs developers who are experts in as many technologies as possible.

Take it easy! Learn a little, but consistently!


Concatenation Operator In Elixir

Elixir is an evolution of Erlang programming language. You can think about that those who created Elixir, also found “The Elixir of Youth”. Don’t do that, I was kidding; “The Elixir of Youth” doesn’t exist! Only if you believe and God and in His Son who died and rose again the 3rd day according to the Scriptures, only then you can have eternal life (in heaven). May God bless you and your family!

The concatenation operator in Elixir is <> (“less than” and “greater than” symbols).

Here is an example in Elixir: first I’ll declare three string variables (two with values and the last one will be with concatenation), and finally I’ll print the result.

eliXir1 = "Hello, "
eliXir2 = "World!"
reliXir = eliXir1 <> eliXir2
IO.puts(reliXir)
# Output: Hello, World!

I think it’s important to mention that in Elixir there are no multi-line comments, only single-line comments. Pretty cool, right?

Elixir has two concatenation operators:

  • <> for strings
  • ++ for lists

What if we used the <> symbols in a more organized way and positioned them differently? The concatenation operator in Ruby is >>.


Concatenation Operator In Ruby

In Ruby, string concatenation can be done using two operators:

  • the + operator
  • the << operator

We’ve already seen the + operator in Java. Now let’s check out the << operator.

The << operator it’s quite interesting, isn’t it? The arrows point to the left, which means that the text on the right will be inserted into the text on the left.

Let’s see an example:

r1 = "Ruby "
r2 = "on "
r3 = "Rails"
puts r1 << r2 << r3
# Output: Ruby on Rails

After that, the value of the r1 variable will become “Ruby on Rails”. If we had used the + operator, the value of the r1 variable will not have changed.

Take a look at this example:

greeting = "Hello, "
greeting << "World!"
puts greeting
# Output: Hello, World!

So, every time when we use the << operator, it will modify the original string.

Ruby is still a powerful programming language worth learning and using in your projects!


Concatenation Operator In Visual Basic

Visual Basic is one of the programming language which remains in the top 10 TIOBE Index over the years. By the way, have you ever programmed in Visual Basic?

It was designed to be easy to learn and use, especially for beginners. If you are a Windows OS user and want to have fun, I recommend learning Visual Basic.

The concatenation operator in Visual Basic is the & (ampersand) symbol.

Here is an example:

Dim firstString As String = "Unu, "
Dim secondString As String = "Doi, "
Dim thirdString As String = "Trei"
Dim fullString As String

fullString = firstString & secondString & thirdString
MsgBox(fullString)
' Ouput: Unu, Doi, Trei

Have fun!


Operator-Free Concatenation

It’s time for a break! Or not? Then it’s time for an example without any operator.

In Tcl programming language, string concatenation happens automatically, without any operator like ., .., +, ++, <>, << or &.

You just put the strings next to each other and Tcl treats them as one string. Cool, right? Let’s see an example:

set helloFirst "Hello,"
set helloSecond "World!"
set helloWorld "$helloFirst $helloSecond"

puts $helloWorld
# Output: Hello, World!

Tcl (pronounced “tickle”) is a flexible, dynamic scripting language that is great for rapid prototyping, automation, GUI development and networking.

It’s good to know that Tcl is an abbreviation for “Tool Command Language” and is cross-platform (Windows, Linux, macOS).


What Is Your Favorite Programming Language?

I don’t have a favorite programming language because I used to work with many of them.

My first programming language, from high-school, was C++. At the University (Engineering), I learned C, C# and Visual C++. At work, first I started with HTML & CSS, just kidding, I started with PHP and JavaScript, then Java, Python, Kotlin.

Right now, I want to improve my skills, especially to understand the paradigm of the programming languages and the logic concepts behind them.

If you don’t have a favorite programming language, it’s not a problem, but I recommend you to learn how to program. Why? Because it’s very healthy for your brain, for your thinking, for you logic, and last but not least, for your future.

God blessed us with life, but also with meaning and purpose! What should we do? We need to learn, accumulate knowledge and live a fulfilling life.

Programming languages are just tools for building stuff. Do you want to build something big? In this era of digitalization, your project idea must have at least a website or a mobile app. Do you want to build them or you want to hire someone else to work for you? It’s your choice. Choose wisely!


Learn & Grow

Thank you for making it this far! I hope this article opened new horizons for you!


“Good is good when it’s well done.”
✍ Horja Robert Emanuel
Digital Transformation Architect

Share This Article
Leave a Comment