Concatenate two strings

From CodeCodex

Related content:

X86 Assembly[edit]

DATA SEGMENT

       STR1 DB "ENTER FIRST STRING HERE ->$"
       STR2 DB "ENTER SECOND STRING HERE ->$"
       STR3 DB "CONCATED STRING :->$"
       STR11 DB "FIRST STRING : ->$"
       STR22 DB "SECOND STRING: ->$"
       INSTR1 DB 20 DUP("$")
       INSTR2 DB 20 DUP("$")
       N DB ?
       N1 DB ?
       NEWLINE DB 10,13,"$"

DATA ENDS

CODE SEGMENT

       ASSUME DS:DATA,CS:CODE

START:

       MOV AX,DATA
       MOV DS,AX
       LEA SI,INSTR1
       LEA DI,INSTR2
GET STRING
       MOV AH,09H
       LEA DX,STR1
       INT 21H
       MOV AH,0AH
       MOV DX,SI
       INT 21H


       MOV AH,09H
       LEA DX,NEWLINE
       INT 21H
       MOV AH,09H
       LEA DX,STR2
       INT 21H
       MOV AH,0AH
       MOV DX,DI
       INT 21H


       MOV AH,09H
       LEA DX,NEWLINE
       INT 21H


PRINT THE STRING
       MOV AH,09H
       LEA DX,STR11
       INT 21H
       MOV AH,09H
       LEA DX,INSTR1+2
       INT 21H
       MOV AH,09H
       LEA DX,NEWLINE
       INT 21H
       MOV AH,09H
       LEA DX,STR22                    
       INT 21H
       MOV AH,09H
       LEA DX,INSTR2+2
       INT 21H
       MOV AH,09H
       LEA DX,NEWLINE
       INT 21H
CONCATINATION OF THE STRING
       LEA SI,INSTR1
       LEA DI,INSTR2
       MOV CX,00
       INC SI
    L1:INC SI
       CMP BYTE PTR[SI],"$"
       JNE L1
       ADD DI,2
       MOV BX,0
     L2:
      
       MOV BL,BYTE PTR[DI]
       MOV BYTE PTR[SI],BL
       
       INC SI
       INC DI
       CMP BYTE PTR[DI],"$"
       JNE L2
    L8:DEC SI
       CMP SI,2
       JNE L8


       MOV AH,09H
       LEA DX,NEWLINE
       INT 21H
       MOV AH,09H
       LEA DX,STR3
       INT 21H
       MOV AH,09H
       LEA DX,NEWLINE
       INT 21H


    L6:
       MOV BL,BYTE PTR[SI]
       MOV AH,02H
       MOV DL,BL
       INT 21H
       INC SI
       CMP BYTE PTR[SI],"$"
       JNE L6
MOV AH,09H
;       LEA DX,INSTR1+2
 ;      INT 21
       MOV AH,4CH
       INT 21H


CODE ENDS END START

Ada[edit]

Foo : Unbounded_String := Foo & Bar;

AppleScript[edit]

 set newString to aString & anotherString

ASP[edit]

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

BASIC[edit]

foobar = foo + bar

C[edit]

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++[edit]

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

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

Common Lisp[edit]

(concatenate 'string a-string another-string)

Objective-C[edit]

 NSString *foobar = [aStringObject stringByAppendingString:anotherStringObject];

alternatively:

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

Another solution:

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

D[edit]

Two strings:

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

Many strings:

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

Delphi[edit]

foobar := 'foo' + 'bar' ;

Erlang[edit]

Foobar = "foo" ++ "bar".

Gibiane[edit]

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

Go[edit]

foobar := foo + bar

Groovy[edit]

Dynamic variable:

foobar = foo + bar

Faster way:

foobar = "$foo$bar"

With static variable:

String foobar = "$foo$bar"

Haskell[edit]

foobar = foo ++ bar

Java[edit]

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[edit]

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

Lua[edit]

foobar = foo .. bar

Matlab[edit]

foobar = ['foo' 'bar'];

Alternatively:

foobar = strcat(foo, bar);

Note: The second method trims spaces, while the first method does not.

Modula-3[edit]

Sample variable declaration:

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

Sample code:

foobar := foo & bar;

OCaml[edit]

let foobar = foo ^ bar

Pascal[edit]

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

Perl[edit]

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

PHP[edit]

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

PL/SQL[edit]

 foobar := foo || bar;

Python[edit]

foobar = foo + bar

Concatenate N strings:

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

Alternatively:

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

R[edit]

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

Rexx[edit]

foobar = foo || bar

Ruby[edit]

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

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

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

Scala[edit]

var foobar: String = foo + bar;

Faster way:

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

Scheme[edit]

(string-append a-string another-string)

Seed7[edit]

foobar := foo & bar;

Smalltalk[edit]

foobar := foo, bar.

Standard ML[edit]

val foobar = foo ^ bar

Tcl[edit]

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

Visual Basic[edit]

foobar = foo & bar

WinBatch[edit]

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

; Alternate 2 with Variable substitution.
strC = "%strA%%strB%" ; "foobar"

Exit
; This code example was written by Detlev Dalitz.20090706.2150.CEST

Zsh[edit]

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

In loop

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

Other titles[edit]

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