Concatenation
From DocForge
Concatenation - from the Latin concatnre, which means to bind, from the Latin catna, chain - literally means conjoining two or more sequences together to form one sequence. Most usually in the context of strings (arrays of characters), this is done quite often and many programming languages have specialized operators for the concatenation of strings and arrays.
Contents |
Programming Languages [edit]
C / C++ [edit]
Using the strcat function:
#include <string.h> char baaz[10] = "foo"; strcat(baaz, " "); strcat(baaz, "bar");
strcat modifies the memory pointed by its first argument, so it must be an editable buffer large enough to hold the resulting concatenated string. Otherwise you will overflow the buffer, possibly crashing the program and creating a security hole in your program.
C++ [edit]
For C++ strings, using the + (plus) operator:
#include <string> std::string foo = "foo"; std::string bar = "bar"; std::string baaz = foo + " " + bar;
D [edit]
Using the ~ (tilde) operator:
char[] baaz = "foo" ~ " " ~ "bar";
Haskell [edit]
Using the ++ (plus plus) list-append operator, since strings are just lists of characters:
baaz = "foo" + " " + "bar"
Java [edit]
Using the + (plus) operator:
String baaz = "foo" + " " + "bar";
OCaml [edit]
Using the ^ (caret) operator:
let baaz = "foo" ^ " " ^ "bar";;
Perl [edit]
Using the . (period) operator:
$baaz = "foo" . " " . "bar";
Or using interpolation:
$foo = "foo"; $bar = "bar"; $baaz = "$foo $bar";
PHP [edit]
Using the . (period) operator:
$baaz = "foo" . " " . "bar";
Or using interpolation:
$foo = "foo"; $bar = "bar"; $baaz = "$foo $bar";
Python [edit]
Using the + (plus) operator:
baaz = "foo" + " " + "bar"
Ruby [edit]
Using the + (plus) operator:
baaz = "foo" + " " + "bar"
Or using interpolation:
foo = "foo" bar = "bar" baaz = "#{foo} #{bar}"
Scheme [edit]
Using the string-append function:
(define baaz (string-append "foo" " " "bar"))

