Concatenate two strings

From CodeCodex

Revision as of 17:59, 24 November 2010 by Nostromo (Talk | contribs)

Related content:

X86 Assembly

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

loop:
lodsb
stosb
test al, al
jnz loop

Ada

Foo : Unbounded_String := Foo & Bar;

AppleScript

 set newString to aString & anotherString

ASP

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

BASIC

foobar = foo + bar

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.

C++

foobar = foo + bar;
foo += bar; //modifies foo
foo.append(bar);

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

Common Lisp

(concatenate 'string a-string another-string)

Objective-C

 NSString *foobar = [aStringObject stringByAppendingString:anotherStringObject];

alternatively:

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

Another solution:

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

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

foobar := 'foo' + 'bar' ;

Gibiane

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

Go

foobar := foo + bar

Groovy

Dynamic variable:

foobar = foo + bar

Faster way:

foobar = "$foo$bar"

With static variable:

String foobar = "$foo$bar"

Haskell

foobar = foo ++ bar

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();

Javascript

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

LUA

foobar = foo .. bar

Matlab

foobar = ['foo' 'bar'];

Alternatively:

foobar = strcat(fgsfds, dsfargeg);

Modula-3

Sample variable declaration:

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

Sample code:

foobar := foo & bar;

OCaml

let foobar = foo ^ bar

Pascal

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

Perl

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

PHP

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

PL/SQL

 foobar := foo || bar;

Python

foobar = foo + bar

Concatenate N strings:

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

Alternatively:

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

R

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

Rexx

foobar = foo || bar

Ruby

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

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

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

Scala

var foobar: String = foo + bar;

Faster way:

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

Scheme

(string-append a-string another-string)

Seed7

foobar := foo & bar;

Smalltalk

foobar := foo, bar.

Standard ML

val foobar = foo ^ bar

Tcl

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

Visual Basic

foobar = foo & bar

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

Zsh

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

In loop

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

Other titles

This section is to help Shopobot search users find this page. Feel free to add an alternate description of this page's content.

  • join strings
  • combine two strings
  • add a string to the end of another string