String append: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Replaced content with "=={{header|Arturo}}== <lang rebol>print join ["Hello" "World"] a: "Hello" 'a ++ "World" print a b: "Hello" append 'b "World" print b c: "Hello" print append c "World"</...")
Line 1: Line 1:
=={{header|Arturo}}==
{{task|Basic language learning}}
<lang rebol>print join ["Hello" "World"]
[[Category:String manipulation]]
[[Category: String manipulation]]
{{basic data operation}}
[[Category:Simple]]


a: "Hello"
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.
'a ++ "World"
print a


b: "Hello"
append 'b "World"
print b


c: "Hello"
;Task:
print append c "World"</lang>
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.
<br><br>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program appendstr64.s */

/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ BUFFERSIZE, 100
/*******************************************/
/* Initialized data */
/*******************************************/
.data
szMessString: .asciz "String :\n"
szString1: .asciz "Alphabet : "
sComplement: .fill BUFFERSIZE,1,0
szString2: .asciz "abcdefghijklmnopqrstuvwxyz"
szCarriageReturn: .asciz "\n"
/*******************************************/
/* UnInitialized data */
/*******************************************/
.bss
/*******************************************/
/* code section */
/*******************************************/
.text
.global main
main:
ldr x0,qAdrszMessString // display message
bl affichageMess
ldr x0,qAdrszString1 // display begin string
bl affichageMess
ldr x0,qAdrszCarriageReturn // display return line
bl affichageMess
ldr x0,qAdrszString1
ldr x1,qAdrszString2
bl append // append sting2 to string1
ldr x0,qAdrszMessString
bl affichageMess
ldr x0,qAdrszString1 // display string
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform system call
qAdrszMessString: .quad szMessString
qAdrszString1: .quad szString1
qAdrszString2: .quad szString2
qAdrszCarriageReturn: .quad szCarriageReturn
/**************************************************/
/* append two strings */
/**************************************************/
/* x0 contains the address of the string1 */
/* x1 contains the address of the string2 */
append:
stp x1,lr,[sp,-16]! // save registers
mov x2,#0 // counter byte string 1
1:
ldrb w3,[x0,x2] // load byte string 1
cmp x3,#0 // zero final ?
add x4,x2,1
csel x2,x4,x2,ne // if x3 not equal 0, x2 = X2 +1 else x2
bne 1b // no -> loop
mov x4,#0 // counter byte string 2
2:
ldrb w3,[x1,x4] // load byte string 2
strb w3,[x0,x2] // store byte string 1
cbz x3,100f // zero final ?
add x2,x2,1 // no -> increment counter 1
add x4,x4,1 // no -> increment counter 2
b 2b // no -> loop
100:

ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</lang>
{{Output}}
<pre>
String :
Alphabet :
String :
Alphabet : abcdefghijklmnopqrstuvwxyz
</pre>
=={{header|Ada}}==
<lang ada>
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_Io; use Ada.Text_IO.Unbounded_IO;

procedure String_Append is
Str : Unbounded_String := To_Unbounded_String("Hello");
begin
Append(Str, ", world!");
Put_Line(Str);
end String_Append;
</lang>
{{out}}
{{out}}
<pre>
Hello, world!
</pre>

=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1.}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.7 algol68g-2.7].}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards).}}
'''File: String_append.a68'''<lang algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #

STRING str := "12345678";
str +:= "9!";
print(str)</lang>
{{out}}
<pre>
123456789!
</pre>

=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program appendstr.s */

/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall

.equ BUFFERSIZE, 100

/* Initialized data */
.data
szMessString: .asciz "String :\n"
szString1: .asciz "Alphabet : "
sComplement: .fill BUFFERSIZE,1,0
szString2: .asciz "abcdefghijklmnopqrstuvwxyz"

szCarriageReturn: .asciz "\n"

/* UnInitialized data */
.bss

/* code section */
.text
.global main
main:

ldr r0,iAdrszMessString @ display message
bl affichageMess
ldr r0,iAdrszString1 @ display begin string
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display line return
bl affichageMess
ldr r0,iAdrszString1
ldr r1,iAdrszString2
bl append @ append sting2 to string1
ldr r0,iAdrszMessString
bl affichageMess
ldr r0,iAdrszString1 @ display string
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess

100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform system call
iAdrszMessString: .int szMessString
iAdrszString1: .int szString1
iAdrszString2: .int szString2
iAdrszCarriageReturn: .int szCarriageReturn
/******************************************************************/
/* append two strings */
/******************************************************************/
/* r0 contains the address of the string1 */
/* r1 contains the address of the string2 */
append:
push {r0,r1,r2,r7,lr} @ save registers
mov r2,#0 @ counter byte string 1
1:
ldrb r3,[r0,r2] @ load byte string 1
cmp r3,#0 @ zero final ?
addne r2,#1
bne 1b @ no -> loop
mov r4,#0 @ counter byte string 2
2:
ldrb r3,[r1,r4] @ load byte string 2
strb r3,[r0,r2] @ store byte string 1
cmp r3,#0 @ zero final ?
addne r2,#1 @ no -> increment counter 1
addne r4,#1 @ no -> increment counter 2
bne 2b @ no -> loop
100:
pop {r0,r1,r2,r7,lr} @ restaur registers
bx lr @ return

/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registers
mov r2,#0 @ counter length */
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call system
pop {r0,r1,r2,r7,lr} @ restaur registers
bx lr @ return


</lang>

=={{header|AutoHotkey}}==
<lang autohotkey>s := "Hello, "
s .= "world."
MsgBox % s</lang>
{{out}}<pre>Hello, world.</pre>

=={{header|Avail}}==
Avail's normal strings are immutable, however string ''variables'' can leverage tuple's appending-assignment method, <code>_↑++=_</code>.

<lang Avail>str : string := "99 bottles of ";
str ++= "beer";
Print: str;</lang>

Note that one can define methods similar to this, thanks to the ''variable occurrence'' message pattern, <code>_↑</code>, whose slot accepts a variable usage and then passes the variable container itself as the corresponding argument. Consider the source for the append used above:

<lang Avail>Public method "_↑++=_" is
[
var : read tuple/write ⊥,
t : tuple
|
var ?= eject var ++ t;
] : ⊤;</lang>
(<code>eject</code> and <code>?=</code> are methods used for unassign-retrieving and assigning to a variable, respectively, only needed when dealing with the containers themselves.)

=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f STRING_APPEND.AWK
BEGIN {
s = "foo"
s = s "bar"
print(s)
exit(0)
}
</lang>
{{out}}
<pre>
foobar
</pre>

=={{header|Axe}}==
<lang axe>Lbl STRCAT
Copy(r₂,r₁+length(r₁),length(r₂)+1)
r₁
Return</lang>


=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<lang BASIC>S$ = "Hello"
S$ = S$ + " World!"
PRINT S$</lang>

==={{header|BaCon}}===
<lang freebasic>
A$ = "Hello"
A$ = A$ & " World!"
PRINT A$
</lang>


==={{header|BBC BASIC}}===
<lang BBC BASIC> S$="Hello"
S$+=" World!"
PRINT S$
END</lang>
{{out}}
<pre>Hello World!</pre>

==={{header|IS-BASIC}}===
<lang IS-BASIC>100 LET S$="Hello"
110 LET S$=S$&" World!"
120 PRINT S$</lang>

=={{header|Bracmat}}==
<lang Bracmat>str="Hello";
str$(!str " World!"):?str;
out$!str;</lang>
{{out}}
<pre>Hello World!</pre>

=={{header|C}}==
<lang c>#include<stdio.h>
#include<string.h>

int main()
{
char str[24]="Good Morning";
char *cstr=" to all";
char *cstr2=" !!!";
int x=0;
//failure when space allocated to str is insufficient.

if(sizeof(str)>strlen(str)+strlen(cstr)+strlen(cstr2))
{
/* 1st method*/
strcat(str,cstr);

/*2nd method*/
x=strlen(str);
sprintf(&str[x],"%s",cstr2);

printf("%s\n",str);

}
return 0;
}</lang>
{{out}}
<pre>Good Morning to all !!!</pre>

=={{header|C sharp|C#}}==
<lang csharp>class Program
{
static void Main(string[] args)
{
string x = "foo";
x += "bar";
System.Console.WriteLine(x);
}
}</lang>

=={{header|C++}}==
<lang cpp>#include <iostream>
#include <string>

int main( ) {
std::string greeting( "Hello" ) ;
greeting.append( " , world!" ) ;
std::cout << greeting << std::endl ;
return 0 ;
}</lang>
{{out}}
<pre>Hello , world!</pre>

=={{header|Clojure}}==
Using global vars.
<lang clojure>user=> (def s "app")
#'user/s
user=> s
"app"
user=> (def s (str s "end"))
#'user/s
user=> s
"append"</lang>

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

=={{header|COBOL}}==
COBOL is not really a variable length field programming language. Most data items are fixed in size at compile time.

This example uses OCCURS DEPENDING ON, and ''reference modification'' to simulate a string append, all within an already maximally bounded character field. This type of programming task, while possible, is not overly common in COBOL applications.

{{works with|GnuCOBOL}}
<lang COBOL> identification division.
program-id. string-append.

data division.
working-storage section.
01 some-string.
05 elements pic x occurs 0 to 80 times depending on limiter.
01 limiter usage index value 7.
01 current usage index.

procedure division.
append-main.

move "Hello, " to some-string

*> extend the limit and move using reference modification
set current to length of some-string
set limiter up by 5
move "world" to some-string(current + 1:)
display some-string

goback.
end program string-append.
</lang>
{{out}}
<pre>$ cobc -xj string-append.cob
Hello, world</pre>

=={{header|CoffeeScript}}==
{{works with|Node.js}}
<lang coffeescript>a = "Hello, "
b = "World!"
c = a + b

console.log c</lang>
Or with concat:
<lang coffeescript>console.log "Hello, ".concat "World!"</lang>
{{out}}
<pre>Hello, World!</pre>

=={{header|Common Lisp}}==
Similar to the [[String append#Racket| Racket]] solution, a macro is necessary to append in-place:
<lang lisp>(defmacro concatenatef (s &rest strs)
"Append additional strings to the first string in-place."
`(setf ,s (concatenate 'string ,s ,@strs)))
(defvar *str* "foo")
(concatenatef *str* "bar")
(format T "~a~%" *str*)
(concatenatef *str* "baz" "abc" "def")
(format T "~a~%" *str*)</lang>

Output:
<pre>foobar
foobarbazabcdef</pre>

=={{header|D}}==
<lang d>import std.stdio;

void main() {
string s = "Hello";
s ~= " world!";
writeln(s);
}</lang>
{{out}}
<pre>Hello world!</pre>

=={{header|Dyalect}}==

<lang Dyalect>var s = "foo"
s += "bar"
print(s)</lang>

=={{header|EasyLang}}==
<lang>a$ = "hello"
a$ &= " world"
print a$</lang>

=={{header|EchoLisp}}==
<lang lisp>
;; Solution from Common Lisp and Racket
(define-syntax-rule (set-append! str tail)
(set! str (string-append str tail)))

(define name "Albert") → name

(set-append! name " de Jeumont-Schneidre")
name
→ "Albert de Jeumont-Schneidre"
</lang>

=={{header|Elena}}==
ELENA 4.x :
<lang elena>import extensions;
import extensions'text;

public program()
{
var s := StringWriter.load("Hello");
s.append:" World";
console.printLine:s.readChar()
}</lang>

=={{header|Elixir}}==
<lang elixir>iex(60)> s = "Hello"
"Hello"
iex(61)> s <> " World!"
"Hello World!"</lang>

=={{header|Emacs Lisp}}==
===version 1===
<lang Emacs Lisp>
(defun glue (str1 str2)
(concat str1 str2) )
</lang>
===version 2===
<lang Emacs Lisp>
(defun glue (str1 str2)
(format "%s%s" str1 str2) )
</lang>
<b>Eval:</b>
<lang Emacs Lisp>
(setq str "Hello, ")
(setq str (glue str "World!") )
(insert str)
</lang>
<b>Output:</b>
<pre>
Hello, World!
</pre>

=={{header|Erlang}}==
{{out}}
<pre>
1> S = "Hello".
"Hello"
2> S ++ " world".
"Hello world"
</pre>

=={{header|Euphoria}}==
<lang euphoria>
sequence string = "String"

printf(1,"%s\n",{string})

string &= " is now longer\n"

printf(1,"%s",{string})
</lang>
{{out}}
<pre>
String
String is now longer
</pre>

=={{header|F_Sharp|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>

=={{header|Factor}}==
<lang factor>"Hello, " "world!" append</lang>
{{out}}
<pre>
"Hello, world!"
</pre>

=={{header|Falcon}}==
<lang falcon>
/* Added by Aykayayciti Earl Lamont Montgomery
April 10th, 2018 */

s1, s2 = "Hello", "Foo"
> s1 + " World"
printl(s2 + " bar")
</lang>
{{out}}
<pre>
Hello World
Foo bar
[Finished in 0.2s]
</pre>

=={{header|Forth}}==
<lang Forth>\ Strings in Forth are simply named memory locations

create astring 256 allot \ create a "string"

s" Hello " astring PLACE \ initialize the string

s" World!" astring +PLACE \ append with "+place"</lang>

Test at the console

<lang> ok
s" Hello " astring place ok
s" World!" astring +place ok
astring count type Hello World! ok
</lang>

=={{header|Fortran}}==

'''Using deferred length character strings:'''

<lang Fortran>
program main

character(len=:),allocatable :: str

str = 'hello'
str = str//' world'

write(*,*) str

end program main
</lang>

{{out}}
<pre>hello world</pre>


'''Using pre-allocated character strings:'''

<lang Fortran>
program str_append
implicit none

character(len=20) :: str

str= 'String'
str(len_trim(str)+1:) = 'Append'
print *, str

end program str_append
</lang>

{{out}}
<pre>StringAppend</pre>

=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64

Var s = "String"
s += " append"
Print s
Sleep</lang>

{{out}}
<pre>
String append
</pre>

=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=0b17e205d56985c8cd1ff108c6fc9ca4 Click this link to run this code]'''
<lang gambas>Public Sub Main()
Dim sString As String = "Hello "

sString &= "World!"
Print sString

End</lang>
Output:
<pre>
Hello World!
</pre>

=={{header|Genie}}==
<lang genie>[indent=4]
/* String append, in Genie */
init
str:string = "Hello"
str += ", world"

print str</lang>
{{out}}
<pre>prompt$ valac stringAppend.gs
prompt$ ./stringAppend
Hello, world</pre>

=={{header|GlovePIE}}==
<lang glovepie>var.string="This is "
var.string+="Sparta!"
debug=var.string</lang>

=={{header|Go}}==
<lang go>s := "foo"
s += "bar"</lang>

=== String Builder ===
The first solution redefines the string variable every time. It might be short in code but it uses much CPU cycles. A better way is to use `string.Builder` but it is not a string. It is more like a buffer which can produce a string. And it really appends the string to the existing variable.
<lang go>
package main

import (
"fmt"
"strings"
)

func main() {
var s strings.Builder
s.WriteString("foo")
s.WriteString("bar")
fmt.Print(s.String())
}
</lang>
{{out}}
foobar

=={{header|Gosu}}==
<lang gosu>// Example 1
var s = "a"
s += "b"
s += "c"
print(s)

// Example 2
print("a" + "b" + "c")

// Example 3
var a = "a"
var b = "b"
var c = "c"
print("${a}${b}${c}")</lang>
{{out}}
<pre>
abc
abc
abc
</pre>

=={{header|Groovy}}==
<lang Groovy>
class Append{
static void main(String[] args){
def c="Hello ";
def d="world";
def e=c+d;
println(e);
}
}
</lang>
{{out}}
<pre>
Hello world
</pre>

=={{header|Haskell}}==

<lang haskell>
main = putStrLn ("Hello" ++ "World")
</lang>

=={{header|Icon}} and {{header|Unicon}}==
In both languages you can:

<lang unicon>
procedure main()
s := "foo"
s ||:= "bar"
write(s)
end
</lang>

Outputs:

<pre>
->ss
foobar
->
</pre>

=={{header|J}}==
<lang j> s=: 'new'
s
new
s=: s,' value' NB. append is in-place
s
new value</lang>

=={{header|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>
{{out}}
<pre>
Hello, World!
Hello, World!
</pre>

=={{header|JavaScript}}==
{{works with|Rhino}}
{{works with|SpiderMonkey}}
<lang JavaScript>var s1 = "Hello";
s1 += ", World!";
print(s1);

var s2 = "Goodbye";
// concat() returns the strings together, but doesn't edit existing string
// concat can also have multiple parameters
print(s2.concat(", World!"));</lang>
{{out}}
<pre>
"Hello, World!"
"Goodbye, World!"
</pre>

=={{header|jq}}==
jq's <code>+</code> operator can be used to append two strings, and under certain circumstances the <code>+=</code> operator can be used as an abbreviation for appending a string to an existing string. For example, all three of the following produce the same output:<lang jq>"Hello" | . += ", world!"

["Hello"] | .[0] += ", world!" | .[0]

{ "greeting": "Hello"} | .greeting += ", world!" | .greeting</lang>
However the <code>+=</code> operator cannot be used with jq variables in the conventional manner. One could nevertheless use the technique illustrated by the following:<lang jq>"Hello" as $a | $a | . += ", world!" as $a | $a</lang>

=={{header|Jsish}}==
From Javascript entry.
<lang javascript>/* String append, in Jsish */
var str = 'Hello';
;str += ', world';

var s2 = 'Goodbye';
;s2.concat(', World!');

/*
=!EXPECTSTART!=
str += ', world' ==> Hello, world
s2.concat(', World!') ==> Goodbye, World!
=!EXPECTEND!=
*/</lang>

{{out}}
<pre>prompt$ jsish --U stringAppend.jsi
str += ', world' ==> Hello, world
s2.concat(', World!') ==> Goodbye, World!</pre>

=={{header|Julia}}==
<lang julia>s = "Hello"
s *= ", world!"</lang>
{{out}}
<pre>"Hello, world!"</pre>

=={{header|Kotlin}}==
<lang kotlin>fun main(args: Array<String>) {
var s = "a"
s += "b"
s += "c"
println(s)
println("a" + "b" + "c")
val a = "a"
val b = "b"
val c = "c"
println("$a$b$c")
}</lang>
{{out}}
<pre>abc
abc
abc</pre>

=={{header|Lambdatalk}}==
In Lambdatalk writing {def name a sequence of words} replaces the sequence of words by the given name in the code string. The name is a word and is not evaluated. Bracketing a name between two curly braces returns its related value. And concatenating named strings is simply done by writing names between curly braces and separated by spaces.
<lang Scheme>
{def christian_name Albert}
-> christian_name
{def name de Jeumont-Schneidre}
-> name

{christian_name} {name}
-> Albert de Jeumont-Schneidre
</lang>

=={{header|langur}}==
<lang langur>var .s = "no more "
.s ~= "foo bars"
writeln .s</lang>

{{out}}
<pre>no more foo bars</pre>

=={{header|Lasso}}==
<lang Lasso>local(x = 'Hello')
#x->append(', World!')
#x</lang>
{{out}}
<pre>Hello, World!</pre>

=={{header|Lingo}}==
<lang lingo>str = "Hello"
put " world!" after str
put str
-- "Hello world!"</lang>

=={{header|LiveCode}}==
Livecode has an "after" keyword for this
<lang LiveCode>local str="live"
put "code" after str</lang>
Output is "livecode"

=={{header|Lua}}==
Not possible as strings are immutable. We can demonstrate their immutability using 'self':
<lang Lua>function string:show ()
print(self)
end

function string:append (s)
self = self .. s
end

x = "Hi "
x:show()
x:append("there!")
x:show()</lang>
{{out}}
<pre>Hi
Hi </pre>
You can of course concatentate them and store the result in the original variable name but that requires a double reference:
<lang Lua>x = "Hi "
x = x .. "there!"
print(x)</lang>
{{out}}
<pre>Hi there!</pre>

=={{header|M2000 Interpreter}}==
Documents in M2000 are objects with paragraphs.

<lang M2000 Interpreter>
a$="ok"
a$+="(one)"
Print a$

Document b$
b$="ok"
b$="(one)"
Print b$
</lang>
{{out}}
<pre>ok(one)
ok(one)
</pre>

=={{header|Maple}}==
<lang maple>a := "Hello";
b := cat(a, " World");
c := `||`(a, " World");</lang>
{{out}}
<pre>
"Hello"
"Hello World"
"Hello World"
</pre>

=={{header|Mathematica}}==
<lang Mathematica>
(* mutable strings are not supported *)
s1 = "testing";
s1 = s1 <> " 123";
s1</lang>
{{out}}
<pre>"testing 123"</pre>

=={{header|min}}==
{{works with|min|0.19.6}}
<lang min>(quote cons "" join) :str-append

"foo" "bar" str-append puts!</lang>
{{out}}
<pre>
foobar
</pre>

=={{header|MontiLang}}==
<lang MontiLang>|Hello | |world!| swap + print</lang>
<lang MontiLang>|Hello | var hello .
|world!| var world .
world hello + print</lang>

=={{header|Nanoquery}}==
<lang Nanoquery>s1 = "this is"
s1 += " a test"

println s1</lang>

{{out}}
<pre>this is a test</pre>

=={{header|Neko}}==
The plus operator +, concats strings.
<lang ActionScript>/**
<doc><p>String append in Neko</pre></doc>
**/

var str = "Hello"
str += ", world"
$print(str, "\n")</lang>
{{out}}
<pre>prompt$ nekoc string-append.neko
prompt$ neko ./string-append.n
Hello, world</pre>

=={{header|NetRexx}}==
<lang NetRexx>s_ = 'Hello'
s_ = s_', world!'
say s_</lang>
{{out}}
<pre>
Hello, world!
</pre>

=={{header|NewLISP}}==
<lang NewLISP>(setq str "foo")

(push "bar" str -1)
; or as an alternative introduced in v.10.1
(extend str "bar")

(println str)
</lang>

=={{header|Nim}}==
<lang nim>var str = "123456"
str.add("78") # two ways
str &= "9!" # to append</lang>

=={{header|NS-HUBASIC}}==
<lang NS-HUBASIC>10 S$ = "HELLO"
20 S$ = S$ + " WORLD!"
30 PRINT S$</lang>

=={{header|Objeck}}==
<lang objeck>
class Append {
function : Main(args : String[]) ~ Nil {
x := "foo";
x->Append("bar");
x->PrintLine();
}
}
</lang>

=={{header|OCaml}}==
<lang ocaml>let () =
let s = Buffer.create 17 in
Buffer.add_string s "Bonjour";
Buffer.add_string s " tout le monde!";
print_endline (Buffer.contents s)</lang>
{{out}}
<pre>Bonjour tout le monde!</pre>

=={{header|Oforth}}==

<lang Oforth>StringBuffer new "Hello, " << "World!" << println</lang>

=={{header|PARI/GP}}==
Not supported in GP.
<lang parigp>s = "Hello";
s = Str(s, ", world!")</lang>
{{out}}
<pre>%1 = "Hello, world!"</pre>

=={{header|Pascal}}==

{{works with|Free Pascal|2.6.2}}

<lang Pascal>program StringAppend;
{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };

var
s: String = 'Hello';
begin
s += ' World !';
WriteLn(S);
ReadLn;
end.</lang>
Output:
<pre>Hello World !</pre>

=={{header|Perl}}==
<lang perl>my $str = 'Foo';
$str .= 'bar';
print $str;</lang>
{{out}}
<pre>Foobar</pre>

=={{header|Phix}}==
<lang Phix>string s = "this string" ?s
s &= " is now longer" ?s</lang>
{{out}}
<pre>
"this string"
"this string is now longer"
</pre>

=={{header|PicoLisp}}==
<lang picolisp>(setq Str1 "12345678")
(setq Str1 (pack Str1 "9!"))
(println Str1)</lang>
{{out}}
<pre>"123456789!"</pre>

=={{header|Pike}}==
<lang Pike>
string msg = "hello";
msg += " world";
write(msg +"\n");
</lang>
{{Out}}
<pre>
hello world
</pre>

=={{header|PL/I}}==
<lang PL/I>Cat: procedure options (main);
declare s character (100) varying;
s = 'dust ';
s ||= 'bowl';
put (s);
end Cat;</lang>
<pre>dust bowl</pre>

=={{header|Plain English}}==
<lang plainenglish>To run:
Start up.
Put "abc" into a string.
Append "123" to the string.
Write the string to the console.
Wait for the escape key.
Shut down.</lang>
{{out}}
<pre>
abc123
</pre>

=={{header|plainTeX}}==
Works with any TeX engine
<lang tex>\def\addtomacro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\foo{Hello}
Initial: \foo

\addtomacro\foo{ world!}
Appended: \foo
\bye</lang>

pdf or dvi output:
<pre>Initial: Hello
Appended: Hello world!
</pre>

=={{header|PowerShell}}==
<lang PowerShell>
$str = "Hello, "
$str += "World!"
$str
</lang>
<pre>Hello, World!</pre>

=={{header|PureBasic}}==
<lang purebasic>S$ = "Hello"
S$ = S$ + " Wo" ;by referencing the string twice
S$ + "rld!" ;by referencing the string once
If OpenConsole()
PrintN(S$)

Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</lang>
Sample output:
<pre>Hello World!</pre>

=={{header|Python}}==
'''File: String_append.py'''<lang python>#!/usr/bin/env python
# -*- coding: utf-8 -*- #

str = "12345678";
str += "9!";
print(str)</lang>
{{out}}
<pre>
123456789!
</pre>

=={{header|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>

{{out}}
<pre>
foo bar
foo bar
</pre>

=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>my $str = "foo";
$str ~= "bar";
say $str;</lang>
{{out}}
<pre>foobar</pre>

=={{header|Relation}}==
<lang Relation>
set a = "Hello"
set b = " World"
set c = a.b
echo c
</lang>

=={{header|REXX}}==
===using abutment===
<lang rexx>s='he'
s=s'llo world!'
Say s</lang>
'''output'''
<pre>
hello world!
</pre>

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

=={{header|Ring}}==
<lang ring>
aString1 = "Welcome to the "
aString2 = "Ring Programming Language"
aString3 = aString1 + aString2
see aString3
</lang>

=={{header|Robotic}}==
<lang robotic>
set "$str1" to "Hello "
inc "$str1" by "world!"
* "&$str1&"
end
</lang>

=={{header|Ruby}}==
<lang ruby>s = "Hello wo"
s += "rld" # new string object
s << "!" # mutates in place, same object
puts s</lang>
{{out}}<pre>Hello world!</pre>

=={{header|Rust}}==
<lang rust>
use std::ops::Add;

fn main(){
let hello = String::from("Hello world");
println!("{}", hello.add("!!!!"));
}</lang>
{{out}}
Hello world!!!!

=== Real append ===
The first solution doesn't append to the string variable. This solution really appends to the existing variable.
<lang rust>
fn main(){
let mut hello = String::from("Hello world");
hello.push_str("!!!!");
println!("{}", hello);
}
</lang>
{{out}}
Hello world!!!!

=={{header|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>

=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";

const proc: main is func
local
var string: str is "12345678";
begin
str &:= "9!";
writeln(str);
end func;</lang>

{{out}}
<pre>
123456789!
</pre>

=={{header|Sidef}}==
<lang ruby>var str = 'Foo';
str += 'bar';
say str;</lang>
{{out}}
<pre>Foobar</pre>

=={{header|SNOBOL4}}==
<lang SNOBOL4> s = "Hello"
s = s ", World!"
OUTPUT = s
END</lang>
{{out}}
<pre>Hello, World!</pre>

=={{header|Stata}}==
<lang stata>sca s="Ars Longa"
sca s=s+" Vita Brevis"
di s

Ars Longa Vita Brevis</lang>

=={{header|Swift}}==
<lang swift>var s = "foo" // "foo"
s += "bar" // "foobar"
print(s) // "foobar"
s.appendContentsOf("baz") // "foobarbaz"
print(s) // "foobarbaz"</lang>

=={{header|Tcl}}==
String concatenation is a fundamental feature of the Tcl language, and there is also an <code>append</code> 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>
{{out}}<pre>hello world</pre>

=={{header|Ursa}}==
<lang ursa>decl string str
set str "hello "

# append "world" to str
set str (+ str "world")

# outputs "hello world"
out str endl console</lang>

=={{header|Vala}}==
<lang vala>void main() {
string x = "foo";
x += "bar\n";
print(x);
}</lang>

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

=={{header|VBScript}}==
<lang vb>s = "Rosetta"
s = s & " Code"
WScript.StdOut.Write s</lang>
{{out}}
<pre>Rosetta Code</pre>

=={{header|Wart}}==
<lang python>s <- "12345678"
s <- (s + "9!")</lang>

=={{header|Wren}}==
<lang javascript>var s = "Hello, "
s = s + "world!"
System.print(s)</lang>

{{out}}
<pre>
Hello, world!
</pre>

=={{header|zkl}}==
zkl strings are immutable, but byte blobs are mutable.
<lang zkl>var s="foo";
s.append("bar"); //-->new string "foobar", var s unchanged
s+="bar"; //-->new string "foobar", var s modifed to new value

s=Data(Void,"foo"); // byte blob/character blob/text editor buffer
s.append("bar"); // or s+="bar"
s.text; //-->"foobar"</lang>


<pre>HelloWorld
{{omit from|bc|No string operations in bc}}
HelloWorld
{{omit from|dc|No string operations in dc}}
HelloWorld
HelloWorld</pre>

Revision as of 06:25, 20 February 2021

Arturo

<lang rebol>print join ["Hello" "World"]

a: "Hello" 'a ++ "World" print a

b: "Hello" append 'b "World" print b

c: "Hello" print append c "World"</lang>

Output:
HelloWorld
HelloWorld
HelloWorld
HelloWorld