String concatenation: Difference between revisions

From Rosetta Code
Content added Content deleted
m (whitespace fixes)
Line 1: Line 1:
{{task|Basic language learning}}[[Category:String manipulation]]{{basic data operation}}Create a string variable equal to any text value. Create another string variable whose value is the original variable concatenated with another string literal.
{{task|Basic language learning}}[[Category:String manipulation]][[Category: String manipulation]]{{basic data operation}}
Create a string variable equal to any text value. Create another string variable whose value is the original variable concatenated with another string literal.


To illustrate the operation, show the content of the variables.
To illustrate the operation, show the content of the variables.
Line 31: Line 32:
end;
end;
end String_Concatenation;</lang>
end String_Concatenation;</lang>
Sample output:
{{out|Sample output}}
<pre>
<pre>
Hello literal
Hello literal
Line 74: Line 75:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}

{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}

{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
<lang algol68>STRING s := "hello";
<lang algol68>STRING s := "hello";
Line 82: Line 81:
STRING s1 := s + " literal";
STRING s1 := s + " literal";
print ((s1, new line))</lang>
print ((s1, new line))</lang>
{{out}}
Output:
<pre>
<pre>
hello literal
hello literal
Line 90: Line 89:
=={{header|BASIC}}==
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}

{{works with|Liberty BASIC}}
{{works with|Liberty BASIC}}
<lang qbasic>s$ = "hello"
<lang qbasic>s$ = "hello"
Line 96: Line 94:
s2$ = s$ + " literal"
s2$ = s$ + " literal"
print s2$</lang>
print s2$</lang>
{{out}}
Output:
<pre>hello literal
<pre>hello literal
hello literal</pre>
hello literal</pre>


=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
<lang bbcbasic> stringvar1$ = "Hello,"
<lang bbcbasic> stringvar1$ = "Hello,"
stringvar2$ = stringvar1$ + " world!"
stringvar2$ = stringvar1$ + " world!"
PRINT "Variable 1 is """ stringvar1$ """"
PRINT "Variable 1 is """ stringvar1$ """"
PRINT "Variable 2 is """ stringvar2$ """"</lang>
PRINT "Variable 2 is """ stringvar2$ """"</lang>
{{out}}
Output:
<pre>Variable 1 is "Hello,"
<pre>Variable 1 is "Hello,"
Variable 2 is "Hello, world!"</pre>
Variable 2 is "Hello, world!"</pre>


=={{header|ZX Spectrum Basic}}==
==={{header|ZX Spectrum Basic}}===
<lang basic>10 LET s$="Hello"
<lang basic>10 LET s$="Hello"
20 LET s$=s$+" World!"
20 LET s$=s$+" World!"
Line 120: Line 118:


=={{header|Bracmat}}==
=={{header|Bracmat}}==

<lang bracmat>"Hello ":?var1
<lang bracmat>"Hello ":?var1
& "World":?var2
& "World":?var2
& str$(!var1 !var2):?var12
& str$(!var1 !var2):?var12
& put$("var1=" !var1 ", var2=" !var2 ", var12=" !var12 "\n")</lang>
& put$("var1=" !var1 ", var2=" !var2 ", var12=" !var12 "\n")</lang>
{{out}}

Output
<pre>var1= Hello , var2= World , var12= Hello World</pre>
<pre>var1= Hello , var2= World , var12= Hello World</pre>


=={{header|Burlesque}}==
=={{header|Burlesque}}==
<lang burlesque>blsq ) "Hello, ""world!"?+

"Hello, world!"</lang>
<lang burlesque>
blsq ) "Hello, ""world!"?+
"Hello, world!"
</lang>


=={{header|C}}==
=={{header|C}}==
Line 174: Line 167:
return 0;
return 0;
}</lang>
}</lang>
{{out}}
Output:
<pre>hello literal
<pre>hello literal
hello literal</pre>
hello literal</pre>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==

<lang csharp>using System;
<lang csharp>using System;


Line 200: Line 192:


=={{header|COBOL}}==
=={{header|COBOL}}==
STRING verb:
With the <code>STRING</code> verb:
<lang cobol> IDENTIFICATION DIVISION.
<lang cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. Concat.
PROGRAM-ID. Concat.
Line 216: Line 208:
GOBACK
GOBACK
.</lang>
.</lang>
Alternate method using the <code>CONCATENATE</code> intrinsic function:

Alternate method using the CONCATENATE intrinsic function:
<lang cobol> ...
<lang cobol> ...
PROCEDURE DIVISION.
PROCEDURE DIVISION.
Line 233: Line 224:
(s (concatenate 'string s s2)))
(s (concatenate 'string s s2)))
(format t "~a~%" s)))</lang>
(format t "~a~%" s)))</lang>

<lang lisp>(defparameter *s* "hello")
<lang lisp>(defparameter *s* "hello")
(print (concatenate 'string *s* " literal"))
(print (concatenate 'string *s* " literal"))
Line 290: Line 280:
end method
end method


end class
end class</lang>
</lang>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang Erlang>
<lang Erlang>S = "hello",
S = "hello",
S1 = S ++ " literal",
S1 = S ++ " literal",
io:format ("~s literal~n",[S]),
io:format ("~s literal~n",[S]),
io:format ("~s~n",[S1])
io:format ("~s~n",[S1])</lang>
{{out|Sample output}}
</lang>
Sample output:
<pre>
<pre>
hello literal
hello literal
Line 314: Line 301:
print (1, s1))
print (1, s1))
puts(1,'\n')</lang>
puts(1,'\n')</lang>
{{out}}

Output:
hello literal
hello literal
hello literal
hello literal
Line 323: Line 309:


=={{header|Fantom}}==
=={{header|Fantom}}==
Illustrating in <tt>fansh</tt>:

<lang fantom>fansh> a := "abc"
Illustrating in fansh:
<lang fantom>
fansh> a := "abc"
abc
abc
fansh> b := a + "def"
fansh> b := a + "def"
Line 333: Line 317:
abc
abc
fansh> b
fansh> b
abcdef
abcdef</lang>
</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 356: Line 339:


=={{header|Gambas}}==
=={{header|Gambas}}==

In gambas, the ampersand symbol is used as a concatenation operator:
In gambas, the ampersand symbol is used as a concatenation operator:
<lang gambas>DIM bestclub AS String

<lang gambas>
DIM bestclub AS String
DIM myconcat AS String
DIM myconcat AS String
bestclub = "Liverpool"
bestclub = "Liverpool"
myconcat = bestclub & " Football Club"
myconcat = bestclub & " Football Club"</lang>
</lang>


=={{header|Go}}==
=={{header|Go}}==
Line 388: Line 367:
fmt.Println(s2)
fmt.Println(s2)
}</lang>
}</lang>
{{out}}
Output:
<pre>
<pre>
hello
hello
Line 407: Line 386:
def s1 = s + "Earthlings"
def s1 = s + "Earthlings"
println s1</lang>
println s1</lang>
{{out}}

Output:
<pre>Greetings Earthlings
<pre>Greetings Earthlings
Greetings Earthlings</pre>
Greetings Earthlings</pre>
Line 434: Line 412:


=={{header|IDL}}==
=={{header|IDL}}==
<lang idl>
<lang idl>s1='Hello'
s1='Hello'
print, s1 + ' literal'
print, s1 + ' literal'
s2=s1 + ' literal'
s2=s1 + ' literal'
print, s2
print, s2</lang>
</lang>


=={{header|J}}==
=={{header|J}}==
Line 448: Line 424:
Some more text!</lang>
Some more text!</lang>
For more info see:
For more info see:
: http://www.jsoftware.com/help/dictionary/d320.htm on <code>,</code>
* http://www.jsoftware.com/help/dictionary/d320.htm on <code>,</code>
: http://www.jsoftware.com/help/dictionary/d500.htm on <code>]</code>
* http://www.jsoftware.com/help/dictionary/d500.htm on <code>]</code>


=={{header|Java}}==
=={{header|Java}}==
Line 460: Line 436:
}
}
}</lang>
}</lang>
{{out}}
Output:
<pre>hello literal
<pre>hello literal
hello literal</pre>
hello literal</pre>
Line 469: Line 445:


=={{header|LabVIEW}}==
=={{header|LabVIEW}}==

The two input on the left are String Controls, the output on the right is a String Indicator. All of them can be placed on the Front Panel. the Concatenate Strings function can be placed on the Block Diagram. You can switch between the Front Panel and the Block Diagram by pressing Ctrl+E.
The two input on the left are String Controls, the output on the right is a String Indicator. All of them can be placed on the Front Panel. the Concatenate Strings function can be placed on the Block Diagram. You can switch between the Front Panel and the Block Diagram by pressing Ctrl+E.


Line 479: Line 454:


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
See BASIC
See [[#BASIC|BASIC]].


=={{header|Lisaac}}==
=={{header|Lisaac}}==
Line 503: Line 478:
<lang logo>make "s "hello
<lang logo>make "s "hello
print word :s "| there!|</lang>
print word :s "| there!|</lang>

=={{header|lua}}==
=={{header|lua}}==
<lang lua>
<lang lua>a = "hello "
a = "hello "
print(a .. "world")
print(a .. "world")
c = a .. "world"
c = a .. "world"
Line 534: Line 509:


=={{header|Mercury}}==
=={{header|Mercury}}==
<lang mercury>:- module string_concat.
<lang>
:- module string_concat.
:- interface.
:- interface.


Line 548: Line 522:
S1 = S ++ " world",
S1 = S ++ " world",
io.write_string(S, !IO), io.nl(!IO),
io.write_string(S, !IO), io.nl(!IO),
io.write_string(S1, !IO), io.nl(!IO).
io.write_string(S1, !IO), io.nl(!IO).</lang>
</lang>


=={{header|MUMPS}}==
=={{header|MUMPS}}==
<lang MUMPS>
<lang MUMPS>STRCAT
STRCAT
SET S="STRING"
SET S="STRING"
WRITE !,S
WRITE !,S
SET T=S_" LITERAL"
SET T=S_" LITERAL"
WRITE !,T
WRITE !,T
QUIT
QUIT</lang>
{{out}}
</lang>
Output:<pre>
<pre>
CACHE>D STRCAT^ROSETTA
CACHE>D STRCAT^ROSETTA
Line 582: Line 554:


=={{header|Metafont}}==
=={{header|Metafont}}==

<lang metafont>string a, b;
<lang metafont>string a, b;
a := "String";
a := "String";
Line 644: Line 615:
say 's7:' s7
say 's7:' s7
</lang>
</lang>
{{out}}
;Output
<pre>
<pre style="height: 15ex; overflow:scroll;">
s1: any text value
s1: any text value
s2: another string literal
s2: another string literal
Line 662: Line 633:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<lang objeck>bundle Default {
bundle Default {
class Repeat {
class Repeat {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
Line 673: Line 643:
}
}
}
}
}</lang>
}
</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Line 713: Line 682:
=={{header|Oz}}==
=={{header|Oz}}==
Strings are lists and are concatenated with the "Append" function. However, often "virtual strings" are used instead. [http://www.mozart-oz.org/home/doc/base/virtualstring.html "Virtual string are designed as a convenient way to combine strings, byte strings, atoms, integers and floats to compound strings without explicit concatenation and conversion"].
Strings are lists and are concatenated with the "Append" function. However, often "virtual strings" are used instead. [http://www.mozart-oz.org/home/doc/base/virtualstring.html "Virtual string are designed as a convenient way to combine strings, byte strings, atoms, integers and floats to compound strings without explicit concatenation and conversion"].

<lang oz>declare
<lang oz>declare
S = "hello"
S = "hello"
{System.showInfo S#" literal"} %% virtual strings are constructed with "#"
{System.showInfo S#" literal"} %% virtual strings are constructed with "#"
S1 = {Append S " literal"}
S1 = {Append S " literal"}
{System.showInfo S1}
{System.showInfo S1}</lang>
</lang>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Line 746: Line 713:
my $s1 = $s . ' literal';
my $s1 = $s . ' literal';
print $s1, "\n";</lang>
print $s1, "\n";</lang>

An example of destructive concatenation:
An example of destructive concatenation:

<lang perl>$s .= ' literal';
<lang perl>$s .= ' literal';
print $s, "\n";</lang>
print $s, "\n";</lang>
Line 754: Line 719:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|#22 "Thousand Oaks"}}
{{works with|Rakudo|#22 "Thousand Oaks"}}

<lang perl6>my $s = 'hello';
<lang perl6>my $s = 'hello';
say $s ~ ' literal';
say $s ~ ' literal';
my $s1 = $s ~ ' literal';
my $s1 = $s ~ ' literal';
say $s1;</lang>
say $s1;</lang>

An example of mutating concatenation:
An example of mutating concatenation:

<lang perl6>$s ~= ' literal';
<lang perl6>$s ~= ' literal';
say $s;</lang>
say $s;</lang>

Note also that most concatenation in Perl is done implicitly via interpolation.
Note also that most concatenation in Perl is done implicitly via interpolation.


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>
<lang PL/I>declare (s, t) character (30) varying;
declare (s, t) character (30) varying;


s = 'hello from me';
s = 'hello from me';
display (s || ' to you.' );
display (s || ' to you.' );
t = s || ' to you all';
t = s || ' to you all';
display (t);
display (t);</lang>
</lang>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Line 800: Line 759:
(let Str2 (pack Str1 " literal")
(let Str2 (pack Str1 " literal")
(prinl Str2) ) )</lang>
(prinl Str2) ) )</lang>

=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>If OpenConsole()
<lang PureBasic>If OpenConsole()
Line 819: Line 779:
s2 = s1 + " world"
s2 = s1 + " world"
print s2</lang>
print s2</lang>
{{out}}

Output:
<pre>hello world
<pre>hello world
hello world</pre>
hello world</pre>
When concatenating many strings, it is more efficient to use the join method of a string object, which takes a list of strings to be joined. The string on which join is called is used as a separator.

When concatenating many strings, it is more efficient to use the join method of a string object, which takes a list of strings to be joined.
The string on which join is called is used as a separator.

<lang python>s1 = "hello"
<lang python>s1 = "hello"
print ", ".join([s1, "world", "mom"])
print ", ".join([s1, "world", "mom"])
Line 832: Line 788:
s2 = ", ".join([s1, "world", "mom"])
s2 = ", ".join([s1, "world", "mom"])
print s2</lang>
print s2</lang>
{{out}}

Output:
<pre>hello, world
<pre>hello, world
hello, world</pre>
hello, world</pre>
Line 854: Line 809:
; hello
; hello
; hello world!</lang>
; hello world!</lang>

=={{header|Raven}}==
=={{header|Raven}}==
<lang Raven># Cat strings
<lang Raven># Cat strings
Line 869: Line 825:
# Heredoc
# Heredoc
" - NOT!!" as $x
" - NOT!!" as $x
"This is the only way to do it%($x)s" print
"This is the only way to do it%($x)s" print</lang>
{{out}}
</lang>
<pre>First string and second string
<pre>First string and second string
First string and second string and third string
First string and second string and third string
Line 906: Line 862:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>/*

<lang rust>
/*
* String concatenation in Rust.
* String concatenation in Rust.
* Copyright by Shlomi Fish, 2013.
* Copyright by Shlomi Fish, 2013.
Line 925: Line 879:
mutable_s += ~" world";
mutable_s += ~" world";
println(fmt!("mutable_s=%s", mutable_s));
println(fmt!("mutable_s=%s", mutable_s));
}</lang>
}

</lang>


=={{header|SAS}}==
=={{header|SAS}}==
Line 948: Line 900:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>
<lang scala>val s = "hello"
val s = "hello"
val s2 = s + " world"
val s2 = s + " world"
println(s2)
println(s2)</lang>
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
Line 974: Line 924:
writeln(s2);
writeln(s2);
end func;</lang>
end func;</lang>
{{out}}

Output:
<pre>
<pre>
hello world
hello world
Line 1,019: Line 968:


=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==

<lang ti89b>"aard" → sv
<lang ti89b>"aard" → sv
Disp sv & "vark"
Disp sv & "vark"
Line 1,032: Line 980:


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<lang tuscript>$$ MODE TUSCRIPT
$$ MODE TUSCRIPT
s = "Hello "
s = "Hello "
print s, "literal"
print s, "literal"


s1 = CONCAT (s,"literal")
s1 = CONCAT (s,"literal")
print s1
print s1</lang>
{{out}}
</lang>
Output:
<pre>
<pre>
Hello literal
Hello literal
Line 1,047: Line 993:


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==

{{works with|Bourne Shell}} {{works with|bash}}
{{works with|Bourne Shell}} {{works with|bash}}

<lang sh>s="hello"
<lang sh>s="hello"
echo "$s literal"
echo "$s literal"
Line 1,058: Line 1,002:
genus='straw'
genus='straw'
fruit=${genus}berry # This outputs the word strawberry
fruit=${genus}berry # This outputs the word strawberry
echo $fruit
echo $fruit</lang>

</lang>


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==
Line 1,068: Line 1,010:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
'''Platform:''' [[.NET]]
'''Platform:''' [[.NET]]

{{works with|Visual Basic .NET|9.0+}}
{{works with|Visual Basic .NET|9.0+}}

<lang vbnet>s = "Hello"
<lang vbnet>s = "Hello"
Console.WriteLine(s & " literal")
Console.WriteLine(s & " literal")
Line 1,106: Line 1,046:
write, var1;
write, var1;
write, var2;</lang>
write, var2;</lang>

[[Category: String manipulation]]

Revision as of 15:45, 27 June 2013

Task
String concatenation
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses

Create a string variable equal to any text value. Create another string variable whose value is the original variable concatenated with another string literal.

To illustrate the operation, show the content of the variables.

ActionScript

<lang actionscript>package {

   public class Str
   {
       public static function main():void
       {
           var s:String = "hello";
           trace(s + " literal");
           var s2:String = s + " literal";
           trace(s2);
       }
   }

}</lang>

Ada

<lang ada>with Ada.Text_IO; use Ada.Text_IO;

procedure String_Concatenation is

  S : String := "Hello";

begin

  Put_Line (S & " literal");
  declare
     S1 : String := S & " literal";
  begin
     Put_Line (S1);
  end;

end String_Concatenation;</lang>

Sample output:
Hello literal
Hello literal

Aime

<lang aime>text s, v;

s = "Hello"; o_text(s); o_newline(); v = cat(s, ", World!"); o_text(v); o_newline();</lang>

Output:
Hello
Hello, World!

AppleScript

<lang applescript>try

   set endMsg to "world!"
   set totMsg to "Hello, " & endMsg
   display dialog totMsg

end try</lang>

AutoHotkey

<lang AutoHotkey>s := "hello" Msgbox, %s% s1 := s . " literal" ;the . is optional Msgbox, %s1%</lang>

AWK

The AWK concatenation operator is nothing. <lang awk>BEGIN {

  s = "hello"
  print s " literal"
  s1 = s " literal"
  print s1

}</lang>

ALGOL 68

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d

<lang algol68>STRING s := "hello"; print ((s + " literal", new line)); STRING s1 := s + " literal"; print ((s1, new line))</lang>

Output:
hello literal
hello literal

BASIC

Works with: QuickBasic version 4.5
Works with: Liberty BASIC

<lang qbasic>s$ = "hello" print s$;" literal" 'or s$ + " literal" s2$ = s$ + " literal" print s2$</lang>

Output:
hello literal
hello literal

BBC BASIC

<lang bbcbasic> stringvar1$ = "Hello,"

     stringvar2$ = stringvar1$ + " world!"
     PRINT "Variable 1 is """ stringvar1$ """"
     PRINT "Variable 2 is """ stringvar2$ """"</lang>
Output:
Variable 1 is "Hello,"
Variable 2 is "Hello, world!"

ZX Spectrum Basic

<lang basic>10 LET s$="Hello" 20 LET s$=s$+" World!" 30 PRINT s$</lang>

Batch File

<lang dos>set string=Hello echo %string% World set string2=%string% World</lang>

Bracmat

<lang bracmat>"Hello ":?var1 & "World":?var2 & str$(!var1 !var2):?var12 & put$("var1=" !var1 ", var2=" !var2 ", var12=" !var12 "\n")</lang>

Output:
var1= Hello  , var2= World , var12= Hello World

Burlesque

<lang burlesque>blsq ) "Hello, ""world!"?+ "Hello, world!"</lang>

C

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <string.h>

char *sconcat(const char *s1, const char *s2) {

 char *s0 = malloc(strlen(s1)+strlen(s2)+1);
 strcpy(s0, s1);
 strcat(s0, s2);
 return s0;

}

int main() {

  const char *s = "hello";
  char *s2;
  
  printf("%s literal\n", s);
  /* or */
  printf("%s%s\n", s, " literal");
  
  s2 = sconcat(s, " literal");
  puts(s2);
  free(s2);

}</lang>

C++

<lang cpp>#include <string>

  1. include <iostream>

int main() {

  std::string s = "hello";
  std::cout << s << " literal" << std::endl;
  std::string s2 = s + " literal";
  std::cout << s2 << std::endl;
  return 0;

}</lang>

Output:
hello literal
hello literal

C#

<lang csharp>using System;

class Program {

   static void Main(string[] args) {
       var s = "hello";
       Console.Write(s);
       Console.WriteLine(" literal");
       var s2 = s + " literal";
       Console.WriteLine(s2);
   }

}</lang>

Clojure

<lang lisp>(def a-str "abcd") (println (str a-str "efgh"))

(def a-new-str (str a-str "efgh")) (println a-new-str)</lang>

COBOL

With the STRING verb: <lang cobol> IDENTIFICATION DIVISION.

      PROGRAM-ID. Concat.
      DATA DIVISION.
      WORKING-STORAGE SECTION.
      01  Str  PIC X(7) VALUE "Hello, ".
      01  Str2 PIC X(15).
      PROCEDURE DIVISION.
          DISPLAY "Str  : " Str
          STRING Str " World!" DELIMITED BY SIZE INTO Str2
          DISPLAY "Str2 : " Str2
          GOBACK
          .</lang>

Alternate method using the CONCATENATE intrinsic function: <lang cobol> ...

      PROCEDURE DIVISION.
          DISPLAY "Str  : " Str
          MOVE FUNCTION CONCATENATE(Str, " World!") TO Str2
          DISPLAY "Str2 : " Str2
          GOBACK
          .</lang>

Common Lisp

<lang lisp>(let ((s "hello"))

   (format t "~a there!~%" s)
   (let* ((s2 " there!")
          (s (concatenate 'string s s2)))
       (format t "~a~%" s)))</lang>

<lang lisp>(defparameter *s* "hello") (print (concatenate 'string *s* " literal")) (defparameter *s1* (concatenate 'string *s* " literal")) (print *s1*)</lang>

D

<lang d>import std.stdio;

void main() {

   string s = "hello";
   writeln(s ~ " world");
   auto s2 = s ~ " world";
   writeln(s2);

}</lang>

Delphi

<lang delphi>program Concat;

{$APPTYPE CONSOLE}

var

 s1, s2: string;

begin

 s1 := 'Hello';
 s2 := s1 + ' literal';
 WriteLn(s1);
 WriteLn(s2);

end.</lang>

DWScript

<lang delphi>var s1 := 'Hello'; var s2 := s1 + ' World';

PrintLn(s1); PrintLn(s2);</lang>

Dylan.NET

<lang Dylan.NET> //to be compiled using dylan.NET v. 11.3.1.3 or later.

  1. refstdasm mscorlib.dll

import System

assembly concatex exe ver 1.3.0.0

class public auto ansi Module1

  method public static void main()
       var s as string = "hello"
       Console::Write(s)
       Console::WriteLine(" literal")
       var s2 as string = s + " literal"
       Console::WriteLine(s2)
 end method

end class</lang>

Erlang

<lang Erlang>S = "hello", S1 = S ++ " literal", io:format ("~s literal~n",[S]), io:format ("~s~n",[S1])</lang>

Sample output:
hello literal
hello literal

Euphoria

<lang Euphoria>sequence s, s1 s = "hello" puts(1, s & " literal") puts(1,'\n') s1 = s & " literal" print (1, s1)) puts(1,'\n')</lang>

Output:
hello literal
hello literal

Factor

<lang factor>"wake up" [ " sheeple" append print ] [ ", you sheep" append ] bi print</lang>

Fantom

Illustrating in fansh: <lang fantom>fansh> a := "abc" abc fansh> b := a + "def" abcdef fansh> a abc fansh> b abcdef</lang>

Forth

Works with: GNU Forth

<lang forth>s" hello" pad place pad count type s" there!" pad +place \ +place is called "append" on some Forths pad count type</lang>

Fortran

<lang fortran>program StringConcatenation

integer, parameter  :: maxstringlength = 64 character (maxstringlength) :: s1, s = "hello"

print *,s // " literal" s1 = trim(s) // " literal" print *,s1

end program</lang>

Gambas

In gambas, the ampersand symbol is used as a concatenation operator: <lang gambas>DIM bestclub AS String DIM myconcat AS String bestclub = "Liverpool" myconcat = bestclub & " Football Club"</lang>

Go

<lang go>package main

import "fmt"

func main() {

   // text assigned to a string variable
   s := "hello"
   // output string variable
   fmt.Println(s)
   // this output requested by original task descrption, although
   // not really required by current wording of task description.
   fmt.Println(s + " literal")
   // concatenate variable and literal, assign result to another string variable
   s2 := s + " literal"
   // output second string variable
   fmt.Println(s2)

}</lang>

Output:
hello
hello literal
hello literal

Golfscript

<lang golfscript>"Greetings ":s; s"Earthlings"+puts s"Earthlings"+:s1; s1 puts</lang>

Groovy

<lang groovy>def s = "Greetings " println s + "Earthlings"

def s1 = s + "Earthlings" println s1</lang>

Output:
Greetings Earthlings
Greetings Earthlings

Haskell

<lang haskell>import System.IO s = "hello" s1 = s ++ " literal" main = do putStrLn (s ++ " literal")

         putStrLn s1</lang>

HicEst

<lang HicEst>CHARACTER s = "hello", sl*100

WRITE() s // " literal" sl = s // " literal" WRITE() sl</lang>

Icon and Unicon

<lang Icon>procedure main() s1 := "hello" write(s2 := s1 || " there.") # capture the reuslt for write(s2) # ... the 2nd write end</lang>

IDL

<lang idl>s1='Hello' print, s1 + ' literal' s2=s1 + ' literal' print, s2</lang>

J

<lang J> s1 =. 'Some '

  ]s1, 'text '

Some text

  ]s2 =. s1 , 'more text!'

Some more text!</lang> For more info see:

Java

<lang java5>public class Str{

  public static void main(String[] args){
     String s = "hello";
     System.out.println(s + " literal");
     String s2 = s + " literal";
     System.out.println(s2);
  }

}</lang>

Output:
hello literal
hello literal

JavaScript

<lang javascript>var s = "hello" print(s + " there!")</lang>

LabVIEW

The two input on the left are String Controls, the output on the right is a String Indicator. All of them can be placed on the Front Panel. the Concatenate Strings function can be placed on the Block Diagram. You can switch between the Front Panel and the Block Diagram by pressing Ctrl+E.

Lang5

<lang lang5>: concat 2 compress "" join ; 'hello " literal" concat</lang>

Liberty BASIC

See BASIC.

Lisaac

<lang Lisaac>Section Header

+ name := STRING_CONCATENATION;

Section Public

- main <- (

 + sc : STRING_CONSTANT;
 + sv : STRING;
 sc := "Hello";
 (sc + " literal").println;
 sv := sc + " literal";
 sv.println;

);</lang>

<lang logo>make "s "hello print word :s "| there!|</lang>

lua

<lang lua>a = "hello " print(a .. "world") c = a .. "world" print(c)</lang>

Mathematica

<lang Mathematica>str= "Hello "; str<>"Literal"</lang>

MATLAB

<lang MATLAB>>> string1 = '1 Fish'

string1 =

1 Fish

>> string2 = [string1 ', 2 Fish, Red Fish, Blue Fish']

string2 =

1 Fish, 2 Fish, Red Fish, Blue Fish</lang>

Maxima

<lang maxima>s: "the quick brown fox"; t: "jumps over the lazy dog"; sconcat(s, " ", t); /* "the quick brown fox jumps over the lazy dog" */</lang>

Mercury

<lang mercury>:- module string_concat.

- interface.
- import_module io.
- pred main(io::di, io::uo) is det.
- implementation.
- import_module string.

main(!IO) :-

   S = "hello",
   S1 = S ++ " world",
   io.write_string(S, !IO), io.nl(!IO),
   io.write_string(S1, !IO), io.nl(!IO).</lang>

MUMPS

<lang MUMPS>STRCAT

SET S="STRING"
WRITE !,S
SET T=S_" LITERAL"
WRITE !,T
QUIT</lang>
Output:
CACHE>D STRCAT^ROSETTA
 
STRING
STRING LITERAL

M4

M4 has macros rather than variables, but a macro expanded can work like a variable. <lang m4>define(`concat',`$1$2')dnl define(`A',`any text value')dnl concat(`A',` concatenated with string literal') define(`B',`concat(`A',` and string literal')')dnl B</lang>

MAXScript

<lang maxscript>s = "hello" print (s + " literal") s1 = s + " literal" print s1</lang>

Metafont

<lang metafont>string a, b; a := "String"; message a & " literal"; b := a & " literal"; message b;</lang>

Modula-3

Strings in Modula-3 are called TEXTs. Concatenation can use &, just like Ada. <lang modula3>MODULE Concat EXPORTS Main;

IMPORT IO;

VAR string: TEXT := "String";

   string1: TEXT;

BEGIN

 IO.Put(string & " literal.\n");
 string1 := string & " literal.\n";
 IO.Put(string1);

END Concat.</lang> Modula-3 also provides modules for dealing with TEXTs, such as Text. <lang modula3>string1 := Text.Concat(string, " literal.\n");</lang>

Nemerle

Can be done with Concat() method or + operator: <lang Nemerle>using System; using System.Console; using Nemerle.Utility.NString; // contains method Concat()

module Stringcat {

   Main() : void
   {
       def text1 = "This string has";
       def cat1  = Concat( " ", [text, "been concatenated"]);
       def cat2  = text1 + " also been concatenated";
       Write($"$cat1\n$cat2\n");
   }

}</lang>

NetRexx

<lang NetRexx>/* NetRexx */

options replace format comments java crossref savelog symbols

s1 = 'any text value' s2 = 'another string literal' s3 = s1 s2 -- concatenate variables with blank space (note that only one blank space is added) s4 = s1 || s2 -- concatenate variables with abuttal (here, no blank spaces are added) s5 = s1 'another string literal' -- concatenate a variable and a literal with blank space s6 = s1'another string literal' -- concatenate a variable and a literal using abuttal s7 = s1 || 'another string literal' -- ditto

say 's1:' s1 -- concatenation with blank space is employed here too say 's2:' s2 say 's3:' s3 say 's4:' s4 say 's5:' s5 say 's6:' s6 say 's7:' s7 </lang>

Output:
s1: any text value
s2: another string literal
s3: any text value another string literal
s4: any text valueanother string literal
s5: any text value another string literal
s6: any text valueanother string literal
s7: any text valueanother string literal

Nimrod

Strings can be concatenated with &. <lang nimrod>var str, str1 = "String" echo(str & " literal.") str1 = str1 & " literal." echo(str1)</lang>

Objeck

<lang objeck>bundle Default {

 class Repeat {
   function : Main(args : String[]) ~ Nil {
     s := "hello";
     s->PrintLine();
     " literal"->PrintLine();
     s->Append(" literal");
     s->PrintLine();
   }
 }

}</lang>

Objective-C

<lang objc>#import <Foundation/Foundation.h>

int main() {

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 NSString *s = @"hello";
 printf("%s%s\n", [s UTF8String], " literal");
 
 NSString *s2 = [s stringByAppendingString:@" literal"];
 // or, NSString *s2 = [NSString stringWithFormat:@"%@%@", s, @" literal"];
 puts([s2 UTF8String]);
 /* or */
 NSMutableString *s3 = [NSMutableString stringWithString: s];
 [s3 appendString: @" literal"];
 puts([s3 UTF8String]);
 
 [pool release];
 return 0;

}</lang>

OCaml

<lang ocaml>let s = "hello" let s1 = s ^ " literal" let () =

 print_endline (s ^ " literal");
 (* or Printf.printf "%s literal\n" s; *)
 print_endline s1</lang>

Openscad

<lang openscad>a="straw"; b="berry"; c=str(a,b); /* Concatenate a and b */ echo (c);</lang>

Oz

Strings are lists and are concatenated with the "Append" function. However, often "virtual strings" are used instead. "Virtual string are designed as a convenient way to combine strings, byte strings, atoms, integers and floats to compound strings without explicit concatenation and conversion". <lang oz>declare S = "hello" {System.showInfo S#" literal"} %% virtual strings are constructed with "#" S1 = {Append S " literal"} {System.showInfo S1}</lang>

PARI/GP

<lang parigp>s = "Hello "; s = Str(s, "world"); \\ Alternately, this could have been: \\ s = concat(s, "world"); print(s);</lang>

Pascal

<lang pascal>Program StringConcat;

 Var
    s, s1   : String;
 

Begin

   s := 'hello';
   writeln(s + ' literal');
   s1 := concat(s, ' literal');
   { s1 := s + ' literal'; works too, with FreePascal }
   writeln(s1);

End.</lang>

Perl

<lang perl>my $s = 'hello'; print $s . ' literal', "\n"; my $s1 = $s . ' literal'; print $s1, "\n";</lang> An example of destructive concatenation: <lang perl>$s .= ' literal'; print $s, "\n";</lang>

Perl 6

Works with: Rakudo version #22 "Thousand Oaks"

<lang perl6>my $s = 'hello'; say $s ~ ' literal'; my $s1 = $s ~ ' literal'; say $s1;</lang> An example of mutating concatenation: <lang perl6>$s ~= ' literal'; say $s;</lang> Note also that most concatenation in Perl is done implicitly via interpolation.

PL/I

<lang PL/I>declare (s, t) character (30) varying;

s = 'hello from me'; display (s || ' to you.' ); t = s || ' to you all'; display (t);</lang>

PowerShell

<lang powershell>$s = "Hello" Write-Host $s World.

  1. alternative, using variable expansion in strings

Write-Host "$s World."

$s2 = $s + " World." Write-Host $s2</lang>

PHP

<lang php><?php $s = "hello"; echo $s . " literal" . "\n"; $s1 = $s . " literal"; echo $s1 . "\n"; ?></lang>

PicoLisp

<lang PicoLisp>(let Str1 "First text"

  (prinl Str1 " literal")
  (let Str2 (pack Str1 " literal")
     (prinl Str2) ) )</lang>

PureBasic

<lang PureBasic>If OpenConsole()

 s$ = "hello"
 PrintN( s$ + " literal")
 s2$ = s$ + " literal"
 PrintN(s2$)
 Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
 Input()
 CloseConsole()

EndIf</lang>

Python

<lang python>s1 = "hello" print s1 + " world"

s2 = s1 + " world" print s2</lang>

Output:
hello world
hello world

When concatenating many strings, it is more efficient to use the join method of a string object, which takes a list of strings to be joined. The string on which join is called is used as a separator. <lang python>s1 = "hello" print ", ".join([s1, "world", "mom"])

s2 = ", ".join([s1, "world", "mom"]) print s2</lang>

Output:
hello, world
hello, world

R

<lang R>hello <- "hello" paste(hello, "literal") # "hello literal" hl <- paste(hello, "literal") #saves concatenates string to a new variable paste("no", "spaces", "between", "words", sep="") # "nospacesbetweenwords"</lang>

Racket

<lang Racket>#lang racket (define hello "hello") (displayln hello)

(define world (string-append hello " " "world" "!")) (displayln world)

outputs
hello
hello world!</lang>

Raven

<lang Raven># Cat strings "First string and " "second string" cat print

  1. Join

[ "First string" "second string" "third string" ] " and " join print

  1. print

[ "First string" "second string" "third string" ] each print

  1. Formatted print

"\n" "Third string" "Second string" "First string" "%s %s %s %s" print

  1. Heredoc

" - NOT!!" as $x "This is the only way to do it%($x)s" print</lang>

Output:
First string and second string
First string and second string and third string
First stringsecond stringthird string
First string Second string Third string 

This is the only way to do it - NOT!!

REBOL

<lang REBOL>s: "hello" print s1: rejoin [s " literal"] print s1</lang>

REXX

<lang rexx>s = "hello" say s "literal" t = s "literal" /* Whitespace between the two strings causes a space in the output */ say t

                       /* The above method works without spaces too */

genus="straw" say genus"berry" /* This outputs strawberry */ say genus || "berry" /* Concatenation using a doublepipe does not cause spaces */</lang>

Retro

<lang Retro>with strings' "hello" "literal" append puts</lang>

Ruby

<lang ruby>s = "hello" puts s + " literal" s1 = s + " literal" puts s1 s1 << " another" # append to s1</lang>

Rust

<lang rust>/*

* String concatenation in Rust.
* Copyright by Shlomi Fish, 2013.
* Released under the MIT/X11 License
* ( http://en.wikipedia.org/wiki/MIT_License ).
* */

fn main() {

   let s = ~"hello";
   println(fmt!("s=%s", s + " world"));
   let s1 = s + ~" world";
   println(fmt!("s1=%s", s1));
   let mut mutable_s = ~"hello";
   mutable_s += ~" world";
   println(fmt!("mutable_s=%s", mutable_s));

}</lang>

SAS

<lang sas>data _null_;

  a="Hello,";
  b="World!";
  c=a !! " " !! b;
  put c;

run;</lang>

Sather

<lang sather>class MAIN is

 main is
   s ::= "hello";
   #OUT + s + " literal\n";
   s2 ::= s + " literal";
   #OUT + s2 + "\n";
 end;

end;</lang>

Scala

<lang scala>val s = "hello" val s2 = s + " world" println(s2)</lang>

Scheme

<lang scheme>(define s "hello") (display (string-append s " literal")) (newline) (define s1 (string-append s " literal")) (display s1) (newline)</lang>

Seed7

<lang seed7>$ include "seed7_05.s7i";

const proc: main is func

 local
   var string: s is "hello";
   var string: s2 is "";
 begin
   writeln(s <& " world");
   s2 := s & " world";
   writeln(s2);
 end func;</lang>
Output:
hello world
hello world

Slate

<lang slate>define: #s -> 'hello'. inform: s ; ' literal'. define: #s1 -> (s ; ' literal'). inform: s1.</lang>

Smalltalk

<lang smalltalk>|s s1| s := 'hello'. (s,' literal') printNl. s1 := s,' literal'. s1 printNl.</lang>

SNOBOL4

<lang snobol> greet1 = "Hello, " output = greet1 greet2 = greet1 "World!" output = greet2 end</lang>

Standard ML

<lang sml>val s = "hello" val s1 = s ^ " literal\n" val () =

 print (s ^ " literal\n");
 print s1</lang>

Tcl

<lang tcl>set s hello puts "$s there!" append s " there!" puts $s</lang> You can also just group the strings to concatenate together at the point where they are used, using Tcl's built-in syntactic concatenation: <lang tcl>set s "Hello " set t "World" set u "!" puts $s$t$u  ;# There is nothing special here about using puts; just an example</lang>

TI-89 BASIC

<lang ti89b>"aard" → sv Disp sv & "vark" sv & "wolf" → sv2</lang>

TorqueScript

<lang Torque>%string = "Hello"; echo(%string); %other = " world!"; echo(%other); echo(%string @ %other);</lang>

TUSCRIPT

<lang tuscript>$$ MODE TUSCRIPT s = "Hello " print s, "literal"

s1 = CONCAT (s,"literal") print s1</lang>

Output:
Hello literal
Hello literal 

UNIX Shell

Works with: Bourne Shell
Works with: bash

<lang sh>s="hello" echo "$s literal" s1="$s literal" # This method only works with a space between the strings echo $s1

  1. To concatenate without the space we need squiggly brackets:

genus='straw' fruit=${genus}berry # This outputs the word strawberry echo $fruit</lang>

UnixPipes

<lang bash>echo "hello"

| xargs -n1 -i echo {} literal</lang>

Visual Basic .NET

Platform: .NET

Works with: Visual Basic .NET version 9.0+

<lang vbnet>s = "Hello" Console.WriteLine(s & " literal") s1 = s + " literal" Console.WriteLine(s1)</lang>

XPL0

<lang XPL0>func Concat(S1, S2, S3); \Concatenate strings: S3:= S1 + S2 char S1, S2, S3; int C, I, J; [I:= 0; repeat C:= S1(I);

       S3(I):= C & $7F;        \remove MSb terminator from first string
       I:= I+1;

until C >= $80; J:= 0; repeat C:= S2(J);

       S3(I+J):= C;
       J:= J+1;

until C >= $80; return S3; ];

code Text=12; char A, B, C(80); [A:= "Hello";

B:= " World!";

Concat(A, B, C); Text(0, C); ]</lang>

Yorick

<lang yorick>var1 = "Hello"; var2 = var1 + ", world!"; write, var1; write, var2;</lang>