This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(June 2024) (Learn how and when to remove this message) |

Informal language theory andcomputer programming,string concatenation is the operation of joiningcharacter strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalizations ofconcatenation theory, also called string theory, string concatenation is aprimitive notion.
In manyprogramming languages, string concatenation is abinaryinfix operator, and in some it is written without an operator. This is implemented in different ways:
+ Example from C#:"Hello, " + "World" has the value"Hello, World".. in PHP,& in Visual Basic,[1] and|| in SQL.[2][3] This has the advantage over reusing+ that it allows implicittype conversion to string."Hello, " "World" has the value"Hello, World".$"{hello}{world}", wherestring hello = "Hello, "; andstring world = "World";String.format("%s%s", "Hello, ", "World")In many scientific publications or standards the concatenation operator notation used is||.[4]
In programming, string concatenation generally occurs at run time, as string values are typically not known until run time. However, in the case of string literals, the values are known at compile time, and thus string concatenation can be done at compile time either viastring literal concatenation or viaconstant folding, a potential run-time optimization.
Informal language theory andpattern matching (includingregular expressions), the concatenation operation on strings is generalised to an operation on sets of strings as follows:
For two sets of stringsS1 andS2, theconcatenationS1S2 consists of all strings of the formvw wherev is a string fromS1 andw is a string fromS2, or formallyS1S2 = {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 byS1w = {vw :v ∈S1 } andvS2 = {vw :w ∈S2 }. In these definitions, the stringvw is the ordinary concatenation of stringsv andw as defined in the introductory section.
For example, ifF = { a, b, c, d, e, f, g, h }, andR = { 1, 2, 3, 4, 5, 6, 7, 8 }, thenFR denotes the set of allchess board coordinates inalgebraic 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 withmultiplication).
The strings over an alphabet, with the concatenation operation, form anassociative algebraic structure called afree monoid. Theidentity element is thenull string.
Sets of strings with concatenation andalternation form asemiring, with concatenationdistributing over alternation. The identity for alternation (the 0 element) is theempty set and identity for concatenation (the 1 element) is the set containing just the null string.
In programming for telephony, concatenation is used to provide dynamic audio feedback to a user. For example, in a "time of day"speaking clock, concatenation is used to give the correct time by playing the appropriate recordings concatenated together. For example: "at the tone, the time will be", "eight", "thirty", "five", "and", "twenty", "five", "seconds".
The recordings themselves exist separately, but playing them one after the other provides a grammatically correct sentence to the listener.
This technique is also used in number change announcements,voice mail systems, or most telephony applications that provide dynamic feedback to the caller (e.g.moviefone,tellme, and others).
Programming for any kind of computerised public address system can also employ concatenation for dynamic public announcements (for example, flights in an airport). The system would archive recorded speech of numbers, routes or airlines, destinations, times, etc. and play them back in a specific sequence to produce a grammatically correct sentence that is announced throughout the facility.
One of the principles ofrelational database design is that the fields of data tables should reflect a single characteristic of the table's subject, which means that they should not contain concatenated strings. When concatenation is desired in a report, it should be provided at the time of running the report. For example, to display the physical address of a certain customer, the data might include building number, street name, building sub-unit number, city name, state/province name, postal code, and country name, e.g., "123 Fake St Apt 4, Boulder, CO 80302, USA", which combines seven fields. However, the customers data table should not use one field to store that concatenated string; rather, the concatenation of the seven fields should happen upon running the report. The reason for such principles is that without them, the entry and updating of large volumes of data becomes error-prone and labor-intensive. Separately entering the city, state, ZIP code, and nation allows data-entry validation (such as detecting an invalid state abbreviation). Then those separate items can be used for sorting or indexing the records, such as all with "Boulder" as the city name.
Inrecreational mathematics, many problems concern the properties of numbers under concatenation of their numerals in somebase. Examples includehome primes (primes obtained by repeatedly factoring the increasing concatenation of prime factors of a given number),Smarandache–Wellin numbers (the concatenations of the firstprime numbers), and theChampernowne andCopeland–Erdős constants (the real numbers formed by the decimal representations of thepositive integers and the prime numbers, respectively).