String concatenation: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Common Lisp: top-level setq is undefined.)
m (Fixed lang tags.)
Line 14: Line 14:
}
}
}
}
}</lang>
}
</lang>


=={{header|Ada}}==
=={{header|Ada}}==
Line 29: Line 28:
Put_Line (S1);
Put_Line (S1);
end;
end;
end String_Concatenation;
end String_Concatenation;</lang>
</lang>
Sample output:
Sample output:
<pre>
<pre>
Line 51: Line 49:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<pre>STRING s := "hello";
<lang algol68>STRING s := "hello";
print ((s + " literal", new line));
print ((s + " literal", new line));
STRING s1 := s + " literal";
STRING s1 := s + " literal";
print ((s1, new line))</pre>
print ((s1, new line))</lang>
Output:<pre>
Output:<lang algol68>hello literal
hello literal
hello literal</lang>
hello literal</pre>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 131: Line 128:
(format t "~a~%" s)))</lang>
(format t "~a~%" s)))</lang>


<lang lisp>
<lang lisp>(defparameter *s* "hello")
(defparameter *s* "hello")
(print (concatenate 'string *s* " literal"))
(print (concatenate 'string *s* " literal"))
(defparameter *s1* (concatenate 'string *s* " literal"))
(defparameter *s1* (concatenate 'string *s* " literal"))
Line 141: Line 137:
writefln(s ~ " world");
writefln(s ~ " world");
auto s2 = s ~ " world";
auto s2 = s ~ " world";
writefln(s2);
writefln(s2);</lang>
</lang>


=={{header|E}}==
=={{header|E}}==
<lang e>
<lang e>def a := "rose"
def a := "rose"
println(a + "bud")
println(a + "bud")
def b := a + "tte"
def b := a + "tte"
println(b)
println(b)</lang>
</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 191: Line 184:


=={{header|J}}==
=={{header|J}}==
<lang J>
<lang J> s1 =. 'Some '
s1 =. 'Some '
]s1, 'text '
]s1, 'text '
Some text
Some text
]s2 =. s1 , 'more text!'
]s2 =. s1 , 'more text!'
Some more text!
Some more text!</lang>
</lang>
For more info see:
For more info see:
http://www.jsoftware.com/help/dictionary/d320.htm on ,
http://www.jsoftware.com/help/dictionary/d320.htm on ,
Line 217: Line 208:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>var s = "hello"
<lang javascript>var s = "hello"
print(s + " there!")
print(s + " there!")</lang>
</lang>


=={{header|Lisaac}}==
=={{header|Lisaac}}==
<lang Lisaac>
<lang Lisaac>Section Header
Section Header


+ name := STRING_CONCATENATION;
+ name := STRING_CONCATENATION;
Line 238: Line 227:
sv.print;
sv.print;
'\n'.print;
'\n'.print;
);
);</lang>
</lang>


=={{header|Logo}}==
=={{header|Logo}}==
make "s "hello
<lang logo>make "s "hello
print word :s "| there!|
print word :s "| there!|</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Line 275: Line 263:


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<pre>s = "hello"
<lang maxscript>s = "hello"
print (s + " literal")
print (s + " literal")
s1 = s + " literal"
s1 = s + " literal"
print s1
print s1</lang>
</pre>


=={{header|Metafont}}==
=={{header|Metafont}}==
Line 376: Line 363:


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


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 399: Line 384:


=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>
<lang slate>define: #s -> 'hello'.
define: #s -> 'hello'.
inform: s ; ' literal'.
inform: s ; ' literal'.
define: #s1 -> (s ; ' literal').
define: #s1 -> (s ; ' literal').
inform: s1.
inform: s1.</lang>
</lang>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Line 432: Line 415:
=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==


<pre style="font-family:'TI Uni'">"aard" → sv
<lang ti89b>"aard" → sv
Disp sv & "vark"
Disp sv & "vark"
sv & "wolf" → sv2</pre>
sv & "wolf" → sv2</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
<pre>s="hello"
<lang bash>s="hello"
echo "$s literal"
echo "$s literal"
s1="$s literal"
s1="$s literal"
echo $s1</pre>
echo $s1</lang>


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==
echo "hello"
<lang bash>echo "hello"
| xargs -n1 -i echo {} literal
| xargs -n1 -i echo {} literal</lang>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==

Revision as of 13:01, 22 November 2009

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

Set a string variable equal to any text value. Print it to the console concatenated with a string literal. Create a new string variable whose value is the other variable concatenated with a string literal. Print this new variable.

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

AutoHotkey

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

AWK

The AWK concatenation operator is just a space. <lang awk>BEGIN {

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

}</lang>

ALGOL 68

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

BASIC

Works with: QuickBasic version 4.5

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

hello literal
hello literal

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) {
       string s = "hello";
       Console.WriteLine(s + " literal");
       string s2 = s + " literal";
       Console.WriteLine(s2);
   }

}</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>string s = "hello"; writefln(s ~ " world"); auto s2 = s ~ " world"; writefln(s2);</lang>

E

<lang e>def a := "rose" println(a + "bud") def b := a + "tte" println(b)</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 (*), parameter  :: s = "hello" character (maxstringlength) :: s1

print *,s // " literal" s1 = s // " literal" print *,s1

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

J

<lang J> s1 =. 'Some '

  ]s1, 'text '

Some text

  ]s2 =. s1 , 'more text!'

Some more text!</lang> For more info see: http://www.jsoftware.com/help/dictionary/d320.htm on , http://www.jsoftware.com/help/dictionary/d500.htm on ]

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>

Lisaac

<lang Lisaac>Section Header

+ name := STRING_CONCATENATION;

Section Public

- main <- (

 + sc : STRING_CONSTANT;
 + sv : STRING;
 sc := "Hello";
 (sc + " literal").print;
 '\n'.print;
 sv := sc + " literal";
 sv.print;
 '\n'.print;

);</lang>

<lang logo>make "s "hello print word :s "| there!|</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>

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>

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>

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 destructive concatenation:

<lang perl6>$s ~= ' literal'; say $s;</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>

Python

<lang python>s = "hello" print s + " literal" s1 = s + " literal" print s1</lang>

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>

Ruby

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

Scheme

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

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>

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>

UNIX Shell

<lang bash>s="hello" echo "$s literal" s1="$s literal" echo $s1</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>