Concatenate two strings

From CodeCodex

Revision as of 22:30, 7 July 2010 by 211.2.129.92 (Talk)

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>

Ada

<HIGHLIGHTSYNTAX> Foo : Unbounded_String := Foo & Bar; </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. You need to #include <string.h> for it to work.

C++

<HIGHLIGHTSYNTAX language="cpp"> foobar = foo + bar; foo += bar; //modifies foo </HIGHLIGHTSYNTAX> assuming that "foo" and "bar" are std::string objects.

sucks

Common Lisp

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

Objective-C

<HIGHLIGHTSYNTAX>

NSString *foobar = [aStringObject stringByAppendingString:anotherStringObject];

</HIGHLIGHTSYNTAX> alternatively: <HIGHLIGHTSYNTAX>

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

</HIGHLIGHTSYNTAX>

another way:

<HIGHLIGHTSYNTAX>

NSMutableString *foobar = [NSMutableString stringWithString:aStringObject];
[foobar appendString: 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>

Go

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

Groovy

Dynamic variable: <HIGHLIGHTSYNTAX> foobar = foo + bar </HIGHLIGHTSYNTAX>

Faster way: <HIGHLIGHTSYNTAX> foobar = "$foo$bar" </HIGHLIGHTSYNTAX>

With static variable: <HIGHLIGHTSYNTAX> String foobar = "$foo$bar" </HIGHLIGHTSYNTAX>

Haskell

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

Java

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

Slightly faster way: <HIGHLIGHTSYNTAX language="java122"> foobar = new StringBuilder(foo).append(bar).toString(); </HIGHLIGHTSYNTAX>

Slower, but thread-safe: <HIGHLIGHTSYNTAX language="java122"> foobar = new StringBuffer(foo).append(bar).toString(); </HIGHLIGHTSYNTAX>

Javascript

<HIGHLIGHTSYNTAX language="javascript"> var foobar = foo + bar; var foobar = [foo,bar].join(); </HIGHLIGHTSYNTAX>

LUA

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

Matlab

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

Alternatively: <HIGHLIGHTSYNTAX language="matlab5"> foobar = strcat(fgsfds, dsfargeg); </HIGHLIGHTSYNTAX>

Modula-3

Sample variable declaration: <HIGHLIGHTSYNTAX> VAR foo: TEXT := "Foo"; VAR bar: TEXT := "Bar"; VAR foobar : TEXT; </HIGHLIGHTSYNTAX> Sample code: <HIGHLIGHTSYNTAX> foobar := foo & bar; </HIGHLIGHTSYNTAX>

OCaml

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

Pascal

<HIGHLIGHTSYNTAX> var foobar : String; foobar := Concat(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;
$foobar = "$foo$bar";
$foo .= $bar;

</HIGHLIGHTSYNTAX>

PL/SQL

<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>

Rexx

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

Ruby

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

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

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

Scala

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

Faster way: <HIGHLIGHTSYNTAX> var foobar: String = foo.concat(bar); </HIGHLIGHTSYNTAX>

Scheme

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

Seed7

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

Smalltalk

<HIGHLIGHTSYNTAX> foobar := foo, bar. </HIGHLIGHTSYNTAX>

Standard ML

<HIGHLIGHTSYNTAX> val foobar = foo ^ bar </HIGHLIGHTSYNTAX>

Tcl

<HIGHLIGHTSYNTAX language="tcltk">

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

  1. or
set a foo
set c ${a}bar

</HIGHLIGHTSYNTAX>

Visual Basic

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

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