There are two operators that can be used to concatenate (join) strings.

PHP

OperatorDescriptionExampleEquivalent to
.Concatenation$a = "Hi" . " there"; 
.=Concatenation assignment$a .= "Hello";$a = $a . "Hello";

Java

OperatorDescriptionExampleEquivalent to
+Concatenationa = "Hi" + " there"; 
+=Concatenation assignmenta += "Hello";a = a + "Hello";

C++

OperatorDescriptionExampleEquivalent to
+Concatenationa = "Hi" + " there"; 
+=Concatenation assignmenta += "Hello";a = a + "Hello";

C#

OperatorDescriptionExampleEquivalent to
+Concatenationa = "Hi" + " there"; 
+=Concatenation assignmenta += "Hello";a = a + "Hello";

Visual Basic

OperatorDescriptionExampleEquivalent to
&Concatenationa = "Hi" & " there" 
&=Concatenation assignmenta &= "Hello"a = a & "Hello"

Python

OperatorDescriptionExampleEquivalent to
+Concatenationa = "Hi" + " there" 
+=Concatenation assignmenta += "Hello"a = a + "Hello"

The following example displays “What’s up, dude?”

PHP

<?php
  $a = "What's ";
  $b = "up, ";
  $c = $a . $b;  
  $c .= "dude?";
       
  echo $c;
?>

Java

public static void main(String[] args) throws java.io.IOException {
  String a, b, c;

  a = "What's ";
  b = "up, ";
  c = a + b;  
  c += "dude?";
       
  System.out.println(c);
}

C++

#include <iostream>
using namespace std;
int main() {
  string a, b, c;

  a = "What's ";
  b = "up, ";
  c = a + b;  
  c += "dude?";
       
  cout << c;
  return 0;
}

C#

static void Main() {
  string a, b, c;

  a = "What's ";
  b = "up, ";
  c = a + b;  
  c += "dude?";
       
  Console.Write(c);
  Console.ReadKey();
}

Visual Basic

Sub Main()
  Dim a, b, c As String

  a = "What's "
  b = "up, "
  c = a & b
  c &= "dude?"

  Console.Write(c)

  Console.ReadKey()
End Sub

Python

a = "What's "
b = "up, "
c = a + b
c += "dude?"

print(c)

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!