In formal language theory and computer programming, concatenation is the operation of joining sequential objects, such as character strings, files, lists or pieces of audio or video, end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalizations of concatenation theory, also called string theory, string concatenation is a primitive notion.
Terminology To concatenate is to join two or more sequential objects together to create a new object, generally without modifying any of the original objects. To append is to modify a sequential object by adding material at the end. Thus to append a to b is to modify b to be equal to the concatenation of the original value of b and a. Despite this, in some high-level programming languages, especially functional and logical ones, append is the operation for concatenating lists or arrays without modifying the originals. To prepend is to modify a sequential object by adding material at the beginning. Thus to prepend a to b is to modify b to be equal to the concatenation of a and the original value of b.
Syntax In many programming languages, string concatenation is a binary infix operator, and in some it is written without an operator. Furthermore, the notation may be specific to string concatenation or support concatenation of various sequential data types depending on the language. Examples include:
Overloading the plus sign +, such as most BASICs and C#: "Hello, " + "World" has the value "Hello, World". Dedicated operator, such as . in PHP, & in Visual Basic, || in SQL and ~ in D. Advantages over reusing + may include supporting implicit type conversion to strings and distinguishing numeric array/list concatenation from vector addition, depending on the language. String literal concatenation, in which adjacent strings are concatenated without any operator. Example from C: "Hello, " "World" has the value "Hello, World". Interpolation, using a special syntax to embed variables or expressions within strings. Example from C#: $"{hello}{world}", where string hello = "Hello, "; and string world = "World"; Formatting, using a string formatter to concatenate. Example from Java: String.format("%s%s", "Hello, ", "World") In many scientific publications or standards the concatenation operator notation used is ||.
Lisp The append function takes zero or more (linked) lists as arguments, and returns the concatenation of these lists.
The append function copies all of its arguments except the last, so its time and space complexity are O(n) for a list of n elements. It may thus be a source of inefficiency if used unnecessarily. The nconc function (called append! in Scheme) concatenates its arguments destructively, altering the last cdr of each non-nil argument except the last, making it point to the next list.
Implementation Append can be defined recursively in terms of cons. The two-argument case in Scheme:
Append can also be implemented using Scheme fold-right or Common Lisp reduce:
Prolog The logic programming language Prolog features a built-in append predicate, which can be implemented as follows:
This predicate can be used for appending, but also for picking lists apart. Calling
yields the solutions:
L = [], R = [1, 2, 3] ; L = [1], R = [2, 3] ; L = [1, 2], R = [3] ; L = [1, 2, 3], R = []
Miranda In Miranda, this right-fold, from Hughes (1989:5-6), has the same semantics (by example) as the Scheme implementation above, for two arguments.
append a b = reduce cons b a
Where reduce is Miranda's name for fold, and cons constructs a list from two values or lists. For example,
append [1,2] [3,4] = reduce cons [3,4] [1,2] = (reduce cons [3,4]) (cons 1 (cons 2 nil)) = cons 1 (cons 2 [3,4])) (replacing cons by cons and nil by [3,4]) = [1,2,3,4]
Haskell In Haskell, this right-fold has the same effect as the Scheme implementation above:
This is essentially a reimplementation of Haskell's ++ operator.
Perl In Perl, the push function is equivalent to the append method, and can be used in the following way.
The end result is a list containing [1, 2, 3] The unshift function appends to the front of a list, rather than the end
The end result is a list containing [2, 3, 1] When opening a file, use the ">>" mode to append rather than over write.
Note that when opening and closing file handles, one should always check the return value.
Python In Python, the infix operator + concatenates its arguments:
The list method extend and the augmented assignment operator += append by modification:
The list method append appends a single element to a list by modification:
Bash In Bash the append redirect is the usage of ">>" for adding a stream to something, like in the following series of shell commands:
The stream "Goodbye world!" is added to the text file written in the first command. The ";" implies the execution of the given commands in order, not simultaneously. So, the final content of the text file is:
Other languages Other high-level programming languages which feature linked lists as primitive data structures have adopted an append. To append lists, as an operator, Haskell uses ++, OCaml uses @. Other languages use the + or ++ symbols to nondestructively concatenate a string, list, or array.
Implementation String concatenation generally occurs at run time. For string literals, it can be done at compile time as a special case of constant folding.
Concatenation of sets of strings In formal language theory and pattern matching (including regular expressions), the concatenation operation on strings is generalised to an operation on sets of strings as follows: For two sets of strings S1 and S2, the concatenation S1S2 consists of all strings of the form vw where v is a string from S1 and w is a string from S2, or formally S1S2 = { vw : v ∈ S1, w ∈ S2 }. Many authors also use concatenation of a string set and a single string, and vice versa, which are defined similarly by S1w = { vw : v ∈ S1 } and vS2 = { vw : w ∈ S2 }. In these definitions, the string vw is the ordinary concatenation of strings v and w as defined in the introductory section. For example, if F = { a, b, c, d, e, f, g, h }, and R = { 1, 2, 3, 4, 5, 6, 7, 8 }, then FR denotes the set of all chess board coordinates in algebraic notation, while eR denotes the set of all coordinates of the kings' file. In this context, sets of strings are often referred to as formal languages. The concatenation operator is usually expressed as simple juxtaposition (as with multiplication).
… excerpt ends here. Continue reading the full article.

