Concatenation

From Wikipedia, the free encyclopedia.

This article is part of the
Programming Language Comparison
series.
General Comparison
Basic Syntax
Basic Instructions
Arrays
Associative arrays
String Operations
String Functions
List comprehension
Object-oriented programming
Object-oriented constructors
Database access

Evaluation strategy
List of "hello world" programs

Comparison of ALGOL 68 and C++
Compatibility of C and C++
Comparison of Pascal and Borland Delphi
Comparison of Pascal and C
Comparison of Java and C++
Comparison of Java and C#
Comparison of C# and Visual Basic .NET
Comparison of ABAP and Java
For concatenation of general lists, see append.

In computer programming, string 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 many programming languages, string concatenation is a binary infix operator.

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

print "Hello " + "World";

Contents

[edit] Different languages

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

[edit] Examples

Operator Symbol name Language
+ plus sign ActionScript, BASIC, C++, C#, Java, JavaScript, Pascal, Python, Ruby, Windows PowerShell, SQL, GML
& ampersand Ada, AppleScript, VHDL, Visual Basic
. dot Perl (before version 6), PHP, Maple (before version 6)
.. double dot Lua
~ tilde Perl 6
++ double plus Haskell
// double slash Fortran
|| double vertical bar REXX, SQL, Maple (since version 6), Icon
$+ dollar plus mIRC Scripting Language

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:

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

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:

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 dollar sign ($) scalar sigil.

[edit] See also

[edit] External links

This is an extract from Wikipedia, the Free Encyclopedia
offerte voli | hoteles | precios | voli | die verzeichnis | annuarie web | stop smoking london