Concatenation

From Wikipedia, the free encyclopedia

Concatenation is a standard operation in computer programming languages (a subset of formal language theory). It is the operation of joining two character strings end to end. For example, the strings "foo" and "bar" may be concatenated to give "foobar". In programming languages, string concatenation is a binary operation usually accomplished by putting a concatenation operator between two strings (operands).

For example, the following expression uses the "+" symbol as the concatenation operator:

   print "Hello " + "World";

which produces the output:

   Hello World

Contents

[edit] Different languages

Different languages use different operators. Most languages use the "+" sign though several deviate from this norm:

[edit] Programming conventions

[edit] Assignment

Many languages, such as PHP and JavaScript have a variant of the assignment operator that allows concatenation and assignment to a variable in one statement.

For example, in PHP and Perl:

//Example 1 (concatenation operator ".")
$var = "Hello ";
$var = $var . "World";

//Example 2 (combined assignment and concatenation ".=")
$var = "Hello ";
$var .= "World";

Both examples produce the same result.

[edit] Interpolation

Some languages, (such as Perl and PHP), support variable interpolation as an alternative form of string concatenation.

For example, in Perl, the concatenation syntax:

   my $stringVar; 
   $stringVar = "World"; 
   print "Hello " . $stringVar; 

can be substituted with the string literal syntax:

   my $stringVar; 
   $stringVar = "World";
   print "Hello $stringVar"; 

since double quoted string literals in Perl indicate scalar variables with the sigil ($) character.

[edit] Other uses of concatenation

In a Unix shell, the cat command can be used to concatenate files. The output of this concatenation can be a new file which consists of the content of two or more other files. However, the cat command is normally used merely to print a single file to the screen.

Since 1987 there has been the annual review called 'The Science Fact & Science Fiction Concatenation'. This was distributed to two major SF conventions: the UK national convention and the European (Eurocon) Convention. Since 1990 Concatenation has been on the web at http://www.concatenation.org. 'Concatenation' here refers to the joining of science fact with fiction and the magazine/website's compilors are scientists who enjoy SF as fiction.

In the cellular industry, mobile phones and their networks sometimes support "concatenated sms" to overcome the limiting number of characters in a text message (usually 160). Using this method, longer messages are split up by the sending device and recombined at the receiving end. Each message is then billed separately. When the feature works properly, it is nearly transparent to the user. However, due to incompatibilities between providers and lack of support in some phone models, there does not appear to be widespread use.

[edit] Concatenation in mathematics

In mathematics, concatenation is the joining of two strings, that is, when a and b are concatenated, they form ab. Concatenation of two strings, a and b is denoted as ab or a||b.