Concatenate two strings

From CodeCodex

Image:Lightbulb.png Related content:
<DPL>

category=String shownamespace=false </DPL>

Contents

[edit] X86 Assembly


mov ecx, -1
xor al, al
lea edi, destination
repnz scasb
lea esi, source

loop:
lodsb
stosb
test al, al
jnz loop

[edit] Ada


Foo : Unbounded_String := Foo & Bar;

[edit] AppleScript


set newString to aString & anotherString

[edit] ASP


Dim sSomeText
sSomeText = "This is some text" & " and this is some more text."

[edit] BASIC


foobar = foo + bar

[edit] C


strcat(destination, source);

The string at "destination" is modified. The programmer must ensure that "destination" points to a block of memory large enough to hold the length of the concatenated string, plus the null terminator; otherwise it will overflow the buffer. You need to #include <string.h> for it to work.

[edit] C++


foobar = foo + bar;
foo += bar; //modifies foo

assuming that "foo" and "bar" are std::string objects.

sucks

[edit] Common Lisp


(concatenate 'string a-string another-string)

[edit] Objective-C


NSString *foobar = [aStringObject stringByAppendingString:anotherStringObject];

alternatively:


NSString *foobar = [NSString stringWithFormat:@"%@%@", aStringObject, anotherStringObject];

another way:


NSMutableString *foobar = [NSMutableString stringWithString:aStringObject];
[foobar appendString: anotherStringObject];

[edit] D

Two strings:

string first = "abc";
string second = "def";
string sum = first ~ second;

Many strings:

string[] parts = ["abc", "def", "ghil"];
auto joined = parts.join("");

[edit] Delphi


foobar := 'foo' + 'bar' ;

[edit] Gibiane


foobar = concat: 'foo' string 'bar' ;

[edit] Go


foobar := foo + bar

[edit] Groovy

Dynamic variable:


foobar = foo + bar

Faster way:


foobar = "$foo$bar"

With static variable:


String foobar = "$foo$bar"

[edit] Haskell


foobar = foo ++ bar

[edit] Java


foobar = foo + bar;

Slightly faster way:


foobar = new StringBuilder(foo).append(bar).toString();

Slower, but thread-safe:


foobar = new StringBuffer(foo).append(bar).toString();

[edit] Javascript


var foobar = foo + bar;
var foobar = [foo,bar].join('');

[edit] LUA


foobar = foo .. bar

[edit] Matlab


foobar = ['foo' 'bar'];

Alternatively:


foobar = strcat(fgsfds, dsfargeg);

[edit] Modula-3

Sample variable declaration:


VAR foo: TEXT := "Foo";
VAR bar: TEXT := "Bar";
VAR foobar : TEXT;

Sample code:


foobar := foo & bar;

[edit] OCaml


let foobar = foo ^ bar

[edit] Pascal


var foobar : String;
foobar := Concat(foo, bar);

[edit] Perl


$foobar = $foo.$bar;  # concatenation
$foo   .= $bar;       # assignment concatenation
$foobar = "$foo$bar"; # interpolation

[edit] PHP


$foobar = $foo.$bar;
$foobar = "$foo$bar";
$foo .= $bar;

[edit] PL/SQL


foobar := foo || bar;

[edit] Python


foobar = foo + bar

Concatenate N strings:


list_of_strings = ['abc', 'def', 'ghi']
foobar = ''.join(list_of_strings)

Alternatively:


 foobar = '%s, %s' % ('abc', 'def')

[edit] R


foo = "string1"
bar = "string2"
foobar = paste(foo,bar,sep="")

[edit] Rexx


foobar = foo || bar

[edit] Ruby

one = "one"
two = "two"
string = one + two

# Alternatively:
string = "#{one}#{two}"

# Or even:
string = [ one, two ].join

[edit] Scala


var foobar: String = foo + bar;

Faster way:


var foobar: String = foo.concat(bar);

[edit] Scheme


(string-append a-string another-string)

[edit] Seed7


foobar := foo & bar;

[edit] Smalltalk


foobar := foo, bar.

[edit] Standard ML


val foobar = foo ^ bar

[edit] Tcl


set a foo
set b bar
set c $a$b

# or
set a foo
set c ${a}bar

[edit] Visual Basic


foobar = foo & bar

[edit] WinBatch

; WinBatch. Concatenate two strings.

strA = "foo"
strB = "bar"

; Traditional string concatenation using the function StrCat ().
strC = StrCat (strA, strB) ; "foobar"

; Alternative string concatenation using the colon character.
; Implemented since WinBatch version WB 2006D, Sep 22, DLL 5.12del.
strD = strA : strB ; "foobar"

Exit
; This code example was written by Detlev Dalitz.20090706.2150.CEST

[edit] Zsh

a=foo
b=bar
c=$a$b

In loop

for i ({1..10}) var+=$i