How to concatenate two arrays in D Programming

Overview

We can use the ~ (tilde) operator to join or concatenate two arrays.

Syntax

array1 ~ array2

Parameters

  • array1: The first array.

  • array2: The second array.

Return value

The value returned is a concatenation of array1 and array2.

Example

Let’s look at the code below:

// import stdio
import std.stdio;
// main method
void main () {
// an array with 5 elements.
double[5] a = 5;
double[5] b = [1, 2, 3, 4, 5];
double [] c;
c = a~b;
writeln("Array c: ",c);
}

Explanation

  • Line 2: We import the std.stdio for input and output operations.
  • Line 5: We create the main method.
  • Lines 7 and 8: We create a and b arrays.
  • Line 9: We create an empty array that holds the concatenation of arrays a and b using the ~ operator.
  • Line 11: We print the values of the array.

Free Resources