String append

From Rosetta Code
Task
String append
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

Most languages provide a way to concatenate two string values, but some languages also provide a convenient way to append in-place to an existing string variable without referring to the variable twice. For this task, create a string variable equal to any text value. Append the string variable with another string literal in the most idiomatic way, without double reference if your language supports it.

Show the contents of the variable after the append operation.

ALGOL 68

Works with: ALGOL 68 version Revision 1.
Works with: ALGOL 68G version Any - tested with release algol68g-2.7.
Works with: ELLA ALGOL 68 version Any (with appropriate job cards).

File: String_append.a68<lang algol68>#!/usr/bin/a68g --script #

  1. -*- coding: utf-8 -*- #

STRING str := "12345678"; str +:= "9!"; print(str)</lang>Output:

123456789!

AWK

<lang AWK>

  1. syntax: GAWK -f STRING_APPEND.AWK

BEGIN {

   s = "foo"
   s = s "bar"
   print(s)
   exit(0)

} </lang>

output:

foobar

BASIC

Applesoft BASIC

<lang BASIC>S$ = "Hello" S$ = S$ + " World!" PRINT S$</lang>

BBC BASIC

<lang BBC BASIC> S$="Hello"

     S$+=" World!"
     PRINT S$
     END</lang>
Output:
Hello World!

C#

<lang csharp>class Program {

   static void Main(string[] args)
   {
       string x = "foo";
       x += "bar";
       System.Console.WriteLine(x);
   }

}</lang>

Clojure

Using global vars. <lang clojure>user=> (def s "app")

  1. 'user/s

user=> s "app" user=> (def s (str s "end"))

  1. 'user/s

user=> s "append"</lang>

Using local bindings. <lang clojure> user=> (let [s "ap", s (str s "pend")] s) "append"</lang>

D

<lang d>import std.stdio;

void main() {

   string s = "Hello";
   s ~= " world!"; 
   writeln(s);

}</lang>

Output:
Hello world!

Erlang

Output:
1> S = "Hello".
"Hello"
2> S ++ " world".
"Hello world"

F#

Strings are immutable in .NET. To append (to the same variable) the variable has to be declared mutable. <lang fsharp>let mutable x = "foo" x <- x + "bar" printfn "%s" x</lang>

Icon and Unicon

In both languages you can:

<lang unicon> procedure main()

   s := "foo"
   s ||:= "bar"
   write(s)

end </lang>

Outputs:

->ss
foobar
->

J

<lang j> s=: 'new'

  s

new

  s=: s,' value'   NB. append is in-place
  s

new value</lang>

Java

<lang Java> String sa = "Hello";

   sa += ", World!";
   System.out.println(sa);
   
   StringBuilder ba = new StringBuilder();
   ba.append("Hello");
   ba.append(", World!");
   System.out.println(ba.toString());

</lang>

Output:
Hello, World!
Hello, World!



Julia

<lang julia>s = "Hello" s *= ", world!"</lang>

Output:
"Hello, world!"

Lasso

<lang Lasso>local(x = 'Hello')

  1. x->append(', World!')
  2. x</lang>
Output:
Hello, World!

NetRexx

<lang NetRexx>s_ = 'Hello' s_ = s_', world!' say s_</lang>

Output:
Hello, world!

Perl

<lang perl>my $str = 'Foo'; $str .= 'bar'; print $str;</lang>

Output:
Foobar

Perl 6

<lang perl6>my $str = "foo"; $str ~= "bar"; say $str;</lang>

Output:
foobar

PL/I

<lang PL/I>Cat: procedure options (main);

  declare s character (100) varying;
  s = 'dust ';
  s ||= 'bowl';
  put (s);

end Cat;</lang>

dust bowl

Python

File: String_append.py<lang python>#!/usr/bin/env python

  1. -*- coding: utf-8 -*- #

str = "12345678"; str += "9!"; print(str)</lang>Output:

123456789!

Racket

<lang racket>;there is no built-in way to set! append in racket (define mystr "foo") (set! mystr (string-append mystr " bar")) (displayln mystr)

but you can create a quick macro to solve that problem

(define-syntax-rule (set-append! str value)

 (set! str (string-append str value)))

(define mymacrostr "foo") (set-append! mymacrostr " bar") (displayln mystr)</lang>

Output:
foo bar
foo bar

REXX

using abutment

<lang rexx>s='he' s=s'llo world!' Say s</lang> output

hello world!

using concatenation

<lang rexx>s="He" s=s || 'llo, World!' /*same as: s=s||'llo, World!' */ say s</lang> output

Hello, World!

Ruby

<lang ruby>s = "Hello wo" s += "rld" # new string object s << "!" # mutates in place, same object puts s</lang>

Output:
Hello world!

Scala

An evaluation in Scala worksheet. <lang scala> var d = "Hello" // Mutables are discouraged //> d  : String = Hello

 d += ", World!" // var contains a totally new re-instantiationed String
 val s = "Hello" // Immutables are recommended   //> s  : String = Hello
 val s1 = s + s                                  //> s1  : String = HelloHello
 val f2 = () => " !" //Function assigned to variable
                                                 //> f2  : () => String = <function0>
 println(s1 + f2());                             //> HelloHello !</lang>

Tcl

String concatenation is a fundamental feature of the Tcl language, and there is also an append that makes concatenation even simpler: <lang tcl>set s "he" set s "${s}llo wo"; # The braces distinguish varname from text to concatenate append s "rld" puts $s</lang>

Output:
hello world

VBA

<lang VBA>Function StringAppend() Dim s As String s = "foo" s = s & "bar" Debug.Print s End Function</lang>

Wart

<lang python>s <- "12345678" s <- (s + "9!")</lang>