Difference between revisions of "Concatenate two strings"

From CodeCodex

(Undo revision 6861 by 193.39.158.203 (talk))
(Erlang)
Line 84: Line 84:
 
<pre>
 
<pre>
 
foobar := 'foo' + 'bar' ;
 
foobar := 'foo' + 'bar' ;
 +
</pre>
 +
 +
==Erlang==
 +
<pre>
 +
Foobar = "foo" ++ "bar".
 
</pre>
 
</pre>
  
Line 138: Line 143:
 
</pre>
 
</pre>
  
==LUA==
+
==Lua==
 
<pre>
 
<pre>
 
foobar = foo .. bar
 
foobar = foo .. bar
Line 223: Line 228:
  
 
==Ruby==
 
==Ruby==
<pre>
+
<pre class="ruby">
 
one = "one"
 
one = "one"
 
two = "two"
 
two = "two"
Line 277: Line 282:
  
 
==Visual Basic==
 
==Visual Basic==
<pre>
+
<pre class="vb">
 
foobar = foo & bar
 
foobar = foo & bar
 
</pre>
 
</pre>
Line 327: Line 332:
 
[[Category:D]]
 
[[Category:D]]
 
[[Category:Delphi]]
 
[[Category:Delphi]]
 +
[[Category:Erlang]]
 
[[Category:Gibiane]]
 
[[Category:Gibiane]]
 
[[Category:Go]]
 
[[Category:Go]]
Line 332: Line 338:
 
[[Category:Haskell]]
 
[[Category:Haskell]]
 
[[Category:Java]]
 
[[Category:Java]]
[[Category:Javascript]]
+
[[Category:JavaScript]]
 
[[Category:Lua]]
 
[[Category:Lua]]
 
[[Category:Matlab]]
 
[[Category:Matlab]]
 +
[[Category:Modula-3]]
 
[[Category:Objective-C]]
 
[[Category:Objective-C]]
 
[[Category:Objective Caml]]
 
[[Category:Objective Caml]]

Revision as of 12:43, 3 December 2010

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

Erlang

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