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

PHP

Operator Description Example Equivalent to
. Concatenation $a = "Hi" . " there"  
.= Concatenation assignment $a .= "Hello" $a = $a . "Hello"

Java

Operator Description Example Equivalent to
+ Concatenation a = "Hi" + " there"  
+= Concatenation assignment a += "Hello" a = a + "Hello"

C++

Operator Description Example Equivalent to
+ Concatenation a = "Hi" + " there"  
+= Concatenation assignment a += "Hello" a = a + "Hello"

C#

Operator Description Example Equivalent to
+ Concatenation a = "Hi" + " there"  
+= Concatenation assignment a += "Hello" a = a + "Hello"

Visual Basic

Operator Description Example Equivalent to
& Concatenation a = "Hi" & " there"  
&= Concatenation assignment a &= "Hello" a = a & "Hello"

Python

Operator Description Example Equivalent to
+ Concatenation a = "Hi" + " there"  
+= Concatenation assignment a += "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!