Concatenation

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer programming, concatenation 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).


Normal concatenation, regarding programming 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] Examples

   +       ;; BASIC, C++, Pascal, JavaScript, Java, Python, Ruby, C#, ActionScript
   &       ;; Ada, AppleScript, VHDL, Visual Basic 
   .       ;; Perl (before version 6), PHP, and Maple (up to version 5)

For a more detailed comparison, please see the concatenation comparison article.

[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: <source lang="php"> //Example 1 (concatenation operator ".") $var = "Hello "; $var = $var . "World";

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

Both examples produce the same result.

[edit] Interpolation

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

For example, in Perl, the concatenation syntax:

<source lang="perl">

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

</source>

can be substituted with the string literal syntax:

<source lang="perl">

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

</source>

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

[edit] See also

[edit] External links

de:Konkatenation (Listen) es:Concatenación fr:Concaténation gl:Concatenación he:שרשור nl:Concatenatie pl:Konkatenacja ru:Конкатенация simple:Concatenation sv:Konkatenering tr:Concatenation zh:串接

Views
Personal tools

Toolbox