Difference between revisions of "Concatenate two strings"

From CodeCodex

Line 163: Line 163:
 
<HIGHLIGHTSYNTAX>
 
<HIGHLIGHTSYNTAX>
 
foobar := foo & bar;
 
foobar := foo & bar;
 +
</HIGHLIGHTSYNTAX>
 +
 +
==Objective-C==
 +
<HIGHLIGHTSYNTAX>
 +
NSString *foobar = [foo stringByAppendingString:bar];
 +
</HIGHLIGHTSYNTAX>
 +
alternatively:
 +
<HIGHLIGHTSYNTAX>
 +
NSString *foobar = [NSString stringWithFormat:@"%@%@", foo, bar];
 
</HIGHLIGHTSYNTAX>
 
</HIGHLIGHTSYNTAX>
  
Line 250: Line 259:
 
<HIGHLIGHTSYNTAX>
 
<HIGHLIGHTSYNTAX>
 
foobar := foo, bar.
 
foobar := foo, bar.
 +
</HIGHLIGHTSYNTAX>
 +
 +
==Standard ML==
 +
<HIGHLIGHTSYNTAX>
 +
val foobar = foo ^ bar
 
</HIGHLIGHTSYNTAX>
 
</HIGHLIGHTSYNTAX>
  
Line 274: Line 288:
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Matlab]]
 
[[Category:Matlab]]
 +
[[Category:Objective-C]]
 
[[Category:Objective Caml]]
 
[[Category:Objective Caml]]
 
[[Category:Perl]]
 
[[Category:Perl]]
Line 283: Line 298:
 
[[Category:Scheme]]
 
[[Category:Scheme]]
 
[[Category:Seed7]]
 
[[Category:Seed7]]
 +
[[Category:Standard ML]]
 
[[Category:Tcl]]
 
[[Category:Tcl]]
 
[[Category:String]]
 
[[Category:String]]

Revision as of 08:16, 22 March 2009

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; </HIGHLIGHTSYNTAX> assuming that "foo" and "bar" are std::string objects.

COBOL

STRING Foo, Bar
    INTO Foobar
END-STRING

Sample "WORKING-STORAGE SECTION."

01 Foo PIC X(3) VALUE "Foo".
01 Bar PIC X(3) VALUE "Bar".
01 Foobar PIC X(6).

Common Lisp

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

Objective-C

<HIGHLIGHTSYNTAX>

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

</HIGHLIGHTSYNTAX>

another way:

<HIGHLIGHTSYNTAX>

NSMutableString *foobar = [NSMutableString stringWithString:@"aStringObject"];
[foobar appendString:@"another string"];

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

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

Objective-C

<HIGHLIGHTSYNTAX> NSString *foobar = [foo stringByAppendingString:bar]; </HIGHLIGHTSYNTAX> alternatively: <HIGHLIGHTSYNTAX> NSString *foobar = [NSString stringWithFormat:@"%@%@", 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

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

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

</HIGHLIGHTSYNTAX>

Visual Basic

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