Concatenation
This article includes a list of references, related reading or external links, but its sources remain unclear because it lacks inline citations.March 2014) (Learn how and when to remove this template message) ( |
Programming language comparisons |
---|
|
|
|
Comparison of individual languages
|
In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In some but not all formalisations of concatenation theory, also called string theory, string concatenation is a primitive notion.
Contents
1 Syntax
2 Implementation
3 Concatenation of sets of strings
4 Algebraic properties
5 Applications
5.1 Audio/telephony
5.2 Database theory
5.3 Recreational mathematics
6 See also
7 References
Syntax
In many programming languages, 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 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.[1] Other syntax exists, like ||
in PL/I and Oracle Database SQL.[2]
In a few languages, notably C, C++, and Python, there is string literal concatenation, meaning that adjacent string literals 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 not in general 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 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).
Algebraic properties
The strings over an alphabet, with the concatenation operation, form an associative algebraic structure with identity element the null string—a free monoid.
Sets of strings with concatenation and alternation form a semiring, with concatenation (*) distributing over alternation (+); 0 is the empty set 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, 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"
- "Two"
- "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.
Database theory
One of the principles of relational 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.
Recreational mathematics
In recreational mathematics, 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 numbers (the concatenations of the first prime numbers), and the Champernowne and Copeland–Erdős constants (the real numbers formed by the decimal representations of the positive integers and the prime numbers, respectively).
See also
- Rope (data structure)
References
^ Concatenation Operators in Visual Basic
^ Concatenation Operator
.mw-parser-output .refbegin{font-size:90%;margin-bottom:0.5em}.mw-parser-output .refbegin-hanging-indents>ul{list-style-type:none;margin-left:0}.mw-parser-output .refbegin-hanging-indents>ul>li,.mw-parser-output .refbegin-hanging-indents>dl>dd{margin-left:0;padding-left:3.2em;text-indent:-3.2em;list-style:none}.mw-parser-output .refbegin-100{font-size:100%}
- John E. Hopcroft and Jeffrey D. Ullman, Introduction to Automata Theory, Languages and Computation, Addison-Wesley Publishing, Reading Massachusetts, 1979. .mw-parser-output cite.citation{font-style:inherit}.mw-parser-output .citation q{quotes:"""""""'""'"}.mw-parser-output .citation .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .citation .cs1-lock-limited a,.mw-parser-output .citation .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .citation .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-ws-icon a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Wikisource-logo.svg/12px-Wikisource-logo.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-maint{display:none;color:#33aa33;margin-left:0.3em}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}
ISBN 0-201-02988-X.
JOHN CORCORAN, WILLIAM FRANK, and MICHAEL MALONEY, String theory, Journal of Symbolic Logic, vol. 39 (1974) pp. 625– 637