String concatenation
   HOME

TheInfoList



OR:

In
formal language theory In logic, mathematics, computer science, and linguistics, a formal language consists of words whose letters are taken from an alphabet and are well-formed according to a specific set of rules. The alphabet of a formal language consists of symb ...
and
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, string concatenation is the operation of joining character strings
end-to-end End-to-end or End to End may refer to: * End-to-end auditable voting systems, a voting system * End-to-end delay, the time for a packet to be transmitted across a network from source to destination * End-to-end encryption, a cryptographic paradigm ...
. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalisations of concatenation theory, also called string theory, string concatenation is a primitive notion.


Syntax

In many
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s, string concatenation is a binary infix operator. The + (plus) operator is often overloaded to denote concatenation for string arguments: "Hello, " + "World" has the value "Hello, World". In other languages there is a separate operator, particularly to specify implicit
type conversion In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point valu ...
to string, as opposed to more complicated behavior for generic plus. Examples include . in Edinburgh IMP, Perl, and PHP, .. in Lua, and & in Ada, AppleScript, and Visual Basic. Other syntax exists, like , , in PL/I and
Oracle Database Oracle Database (commonly referred to as Oracle DBMS, Oracle Autonomous Database, or simply as Oracle) is a multi-model database management system produced and marketed by Oracle Corporation. It is a database commonly used for running online ...
SQL. In a few languages, notably C, C++, and Python, there is string literal concatenation, meaning that adjacent
string literal A string literal or anonymous string is a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally " bracketed delimiters", as in x = "foo", where "foo" is a string ...
s are concatenated, without any operator: "Hello, " "World" has the value "Hello, World". In other languages, concatenation of string literals with an operator is evaluated at compile time, via constant folding, although this is often an implementation detail of the compiler, rather than a language feature.


Implementation

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 via string literal concatenation or via constant folding.


Concatenation of sets of strings

In
formal language theory In logic, mathematics, computer science, and linguistics, a formal language consists of words whose letters are taken from an alphabet and are well-formed according to a specific set of rules. The alphabet of a formal language consists of symb ...
and
pattern matching In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact: "either it will or will not be ...
(including
regular expressions A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" o ...
), the concatenation operation on strings is generalised to an operation on sets of strings as follows: For two sets of strings ''S''1 and ''S''2, the ''concatenation'' ''S''1''S''2 consists of all strings of the form ''vw'' where ''v'' is a string from ''S''1 and ''w'' is a string from ''S''2, or formally . Many authors also use concatenation of a string set and a single string, and vice versa, which are defined similarly by and . In these definitions, the string ''vw'' is the ordinary concatenation of strings ''v'' and ''w'' as defined in the introductory section. For example, if , and , then ''FR'' denotes the set of all chess board coordinates in algebraic notation, while e''R'' 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 Multiplication (often denoted by the Multiplication sign, cross symbol , by the mid-line #Notation and terminology, dot operator , by juxtaposition, or, on computers, by an asterisk ) is one of the four Elementary arithmetic, elementary Op ...
).


Algebraic properties

The strings over an alphabet, with the concatenation operation, form an
associative In mathematics, the associative property is a property of some binary operations, which means that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a valid rule of replacement ...
algebraic structure with identity element the
null string In formal language theory, the empty string, or empty word, is the unique string of length zero. Formal theory Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special ca ...
—a
free monoid In abstract algebra, the free monoid on a set is the monoid whose elements are all the finite sequences (or strings) of zero or more elements from that set, with string concatenation as the monoid operation and with the unique sequence of zero ele ...
. Sets of strings with concatenation and alternation form a
semiring In abstract algebra, a semiring is an algebraic structure similar to a ring, but without the requirement that each element must have an additive inverse. The term rig is also used occasionally—this originated as a joke, suggesting that rigs ar ...
, with concatenation (*) distributing over alternation (+); 0 is the
empty set In mathematics, the empty set is the unique set having no elements; its size or cardinality (count of elements in a set) is zero. Some axiomatic set theories ensure that the empty set exists by including an axiom of empty set, while in othe ...
and 1 the set consisting of just the null string.


Applications


Audio/telephony

In programming for telephony, concatenation is used to provide dynamic audio feedback to a user. For example, in a "time of day"
speaking clock A speaking clock or talking clock is a live or recorded human voice service, usually accessed by telephone, that gives the correct time. The first telephone speaking clock service was introduced in France, in association with the Paris Observ ...
, 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 A voicemail system (also known as voice message or voice bank) is a computer-based system that allows users and subscribers to exchange personal voice messages; to select and deliver voice information; and to process transactions relating to ind ...
systems, or most telephony applications that provide dynamic feedback to the caller (e.g.
moviefone Moviefone is an American-based moving pictures listing and information service. Moviegoers can obtain local showtimes, cinema information, film reviews, and advance tickets, as well as TV content and a comprehensive search tool that allows users ...
, 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.


Database theory

One of the principles of
relational database A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relati ...
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.


Recreational mathematics

In
recreational mathematics Recreational mathematics is mathematics carried out for recreation (entertainment) rather than as a strictly research and application-based professional activity or as a part of a student's formal education. Although it is not necessarily limited ...
, many problems concern the properties of numbers under concatenation of their numerals in some base. Examples include home primes (primes obtained by repeatedly factoring the increasing concatenation of prime factors of a given number),
Smarandache–Wellin number In mathematics, a Smarandache–Wellin number is an integer that in a given base is the concatenation of the first ''n'' prime numbers written in that base. Smarandache–Wellin numbers are named after Florentin Smarandache and Paul R. ...
s (the concatenations of the first
prime number A prime number (or a prime) is a natural number greater than 1 that is not a Product (mathematics), product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime ...
s), and the Champernowne and
Copeland–Erdős constant The Copeland–Erdős constant is the concatenation of "0." with the base 10 representations of the prime numbers in order. Its value, using the modern definition of prime, is approximately :0.235711131719232931374143… . The constant is irration ...
s (the real numbers formed by the decimal representations of the
positive integers In mathematics, the natural numbers are those numbers used for counting (as in "there are ''six'' coins on the table") and ordering (as in "this is the ''third'' largest city in the country"). Numbers used for counting are called '' cardinal ...
and the prime numbers, respectively).


See also

* Rope (data structure)


References

* * {{refend Formal languages Operators (programming) String (computer science)