Concatenate two strings

From CodeCodex

Revision as of 03:24, 27 February 2008 by Spoon! (Talk | contribs)

Related content:

X86 Assembly

<HIGHLIGHTSYNTAX language="asm_x86"> mov ecx, -1 xor al, al lea edi, destination repnz scasb lea esi, source

loop: lodsb stosb test al, al jnz loop </HIGHLIGHTSYNTAX>

AppleScript

<HIGHLIGHTSYNTAX>

set newString to aString & anotherString

</HIGHLIGHTSYNTAX>

ASP

<HIGHLIGHTSYNTAX language="aspvb"> Dim sSomeText sSomeText = "This is some text" & " and this is some more text." </HIGHLIGHTSYNTAX>

BASIC

<HIGHLIGHTSYNTAX> foobar = foo + bar </HIGHLIGHTSYNTAX>

C

<HIGHLIGHTSYNTAX language="c"> strcat(destination, source); </HIGHLIGHTSYNTAX> 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.

C++

<HIGHLIGHTSYNTAX language="cpp"> foobar = foo + bar; </HIGHLIGHTSYNTAX>

Common Lisp

<HIGHLIGHTSYNTAX language="lisp"> (concatenate 'string a-string another-string) </HIGHLIGHTSYNTAX>

Objective-C

<HIGHLIGHTSYNTAX>

NSString *foobar = [@"aStringObject" stringByAppendingString:@"anotherStringObject"];

</HIGHLIGHTSYNTAX>

D

Two strings:

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

Many strings:

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

Delphi

<HIGHLIGHTSYNTAX> foobar := foo + bar ; </HIGHLIGHTSYNTAX>

Gibiane

<HIGHLIGHTSYNTAX> foobar = concat: 'foo' string 'bar' ; </HIGHLIGHTSYNTAX>

Haskell

<HIGHLIGHTSYNTAX language="haskell"> foobar = foo ++ bar </HIGHLIGHTSYNTAX>

Java

<HIGHLIGHTSYNTAX language="java122"> foobar = foo + bar; </HIGHLIGHTSYNTAX>

Javascript

<HIGHLIGHTSYNTAX language="javascript"> var foobar = foo + bar; </HIGHLIGHTSYNTAX>

Matlab

<HIGHLIGHTSYNTAX language="matlab5"> foobar = ['foo' 'bar']; </HIGHLIGHTSYNTAX>

OCaml

<HIGHLIGHTSYNTAX language="ocaml"> let foobar = foo ^ bar </HIGHLIGHTSYNTAX>

Perl

<HIGHLIGHTSYNTAX language="perl">

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

</HIGHLIGHTSYNTAX>

PHP

<HIGHLIGHTSYNTAX language="php3">

$foobar = $foo.$bar;

</HIGHLIGHTSYNTAX>

Python

<HIGHLIGHTSYNTAX language="python"> foobar = foo + bar </HIGHLIGHTSYNTAX> Concatenate N strings: <HIGHLIGHTSYNTAX language="python"> list_of_strings = ['abc', 'def', 'ghi'] foobar = .join(list_of_strings) </HIGHLIGHTSYNTAX>

Alternatively: <HIGHLIGHTSYNTAX language="python">

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

</HIGHLIGHTSYNTAX>

R

<HIGHLIGHTSYNTAX> foo = "string1" bar = "string2" foobar = paste(foo,bar,sep="") </HIGHLIGHTSYNTAX>

Ruby

<HIGHLIGHTSYNTAX> string = "one "+"string" </HIGHLIGHTSYNTAX>

Scheme

<HIGHLIGHTSYNTAX language="scheme"> (string-append a-string another-string) </HIGHLIGHTSYNTAX>

Seed7

<HIGHLIGHTSYNTAX> foobar := foo & bar; </HIGHLIGHTSYNTAX>

Tcl

<HIGHLIGHTSYNTAX language="tcltk">

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

</HIGHLIGHTSYNTAX>

Visual Basic

<HIGHLIGHTSYNTAX> foobar = foo & bar </HIGHLIGHTSYNTAX>