String append: Difference between revisions
Drkameleon (talk | contribs) Undo revision 324765 by Drkameleon (talk) |
Langurmonkey (talk | contribs) |
||
(58 intermediate revisions by 40 users not shown) | |||
Line 15: | Line 15: | ||
Show the contents of the variable after the append operation. |
Show the contents of the variable after the append operation. |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">V s = ‘12345678’ |
|||
s ‘’= ‘9!’ |
|||
print(s)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
123456789! |
|||
</pre> |
|||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program appendstr64.s */ |
/* program appendstr64.s */ |
||
Line 104: | Line 116: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 112: | Line 124: | ||
Alphabet : abcdefghijklmnopqrstuvwxyz |
Alphabet : abcdefghijklmnopqrstuvwxyz |
||
</pre> |
</pre> |
||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!"> |
|||
CHAR ARRAY S1,S2 |
|||
Proc Main() |
|||
S1="Hello, " |
|||
S2="world!"; |
|||
Sassign(S1,S2,S1(0)+1) |
|||
Print(S1) |
|||
Return |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Hello, world! |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada"> |
||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
||
with Ada.Text_IO.Unbounded_Io; use Ada.Text_IO.Unbounded_IO; |
with Ada.Text_IO.Unbounded_Io; use Ada.Text_IO.Unbounded_IO; |
||
Line 123: | Line 153: | ||
Put_Line(Str); |
Put_Line(Str); |
||
end String_Append; |
end String_Append; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 133: | Line 163: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.7 algol68g-2.7].}} |
{{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).}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards).}} |
||
'''File: String_append.a68'''< |
'''File: String_append.a68'''<syntaxhighlight lang="algol68">#!/usr/bin/a68g --script # |
||
# -*- coding: utf-8 -*- # |
# -*- coding: utf-8 -*- # |
||
STRING str := "12345678"; |
STRING str := "12345678"; |
||
str +:= "9!"; |
str +:= "9!"; |
||
print(str)</ |
print(str)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
123456789! |
123456789! |
||
</pre> |
|||
=={{header|Amazing Hopper}}== |
|||
<syntaxhighlight lang="c"> |
|||
#include <jambo.h> |
|||
Main |
|||
r="un corderito", s="María tenía", t="felpudo" |
|||
Multicat ( s," ",r," ",t ),Get utf8, and print it |
|||
d=0 |
|||
Let ' d := Utf8( Cat( Cat ( s, " un " ), t ) )' |
|||
Printnl ( "\n",d ) |
|||
End |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
María tenía un corderito felpudo |
|||
María tenía un felpudo |
|||
</pre> |
</pre> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program appendstr.s */ |
/* program appendstr.s */ |
||
Line 244: | Line 294: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AppleScript}}== |
|||
<syntaxhighlight lang="applescript"> |
|||
set {a, b} to {"Apple", "Script"} |
|||
set a to a & b |
|||
return a as string |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
"AppleScript" |
|||
</pre> |
|||
=={{header|APL}}== |
|||
<syntaxhighlight lang="apl"> s←'hello' |
|||
s,'world' |
|||
helloworld</syntaxhighlight> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">print join ["Hello" "World"] |
||
a: "Hello" |
a: new "Hello" |
||
'a ++ "World" |
'a ++ "World" |
||
print a |
print a |
||
b: "Hello" |
b: new "Hello" |
||
append 'b "World" |
append 'b "World" |
||
print b |
print b |
||
c: "Hello" |
c: "Hello" |
||
print append c "World"</ |
print append c "World"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 266: | Line 333: | ||
HelloWorld |
HelloWorld |
||
HelloWorld</pre> |
HelloWorld</pre> |
||
=={{header|Asymptote}}== |
|||
<syntaxhighlight lang="asymptote">string s = "Hello"; |
|||
s = s + " Wo"; |
|||
s += "rld!"; |
|||
write(s);</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">s := "Hello, " |
||
s .= "world." |
s .= "world." |
||
MsgBox % s</ |
MsgBox % s</syntaxhighlight> |
||
{{out}}<pre>Hello, world.</pre> |
{{out}}<pre>Hello, world.</pre> |
||
Line 276: | Line 349: | ||
Avail's normal strings are immutable, however string ''variables'' can leverage tuple's appending-assignment method, <code>_↑++=_</code>. |
Avail's normal strings are immutable, however string ''variables'' can leverage tuple's appending-assignment method, <code>_↑++=_</code>. |
||
< |
<syntaxhighlight lang="avail">str : string := "99 bottles of "; |
||
str ++= "beer"; |
str ++= "beer"; |
||
Print: str;</ |
Print: str;</syntaxhighlight> |
||
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: |
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: |
||
< |
<syntaxhighlight lang="avail">Public method "_↑++=_" is |
||
[ |
[ |
||
var : read tuple/write ⊥, |
var : read tuple/write ⊥, |
||
Line 288: | Line 361: | ||
| |
| |
||
var ?= eject var ++ t; |
var ?= eject var ++ t; |
||
] : ⊤;</ |
] : ⊤;</syntaxhighlight> |
||
(<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.) |
(<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}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f STRING_APPEND.AWK |
# syntax: GAWK -f STRING_APPEND.AWK |
||
BEGIN { |
BEGIN { |
||
Line 300: | Line 373: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 307: | Line 380: | ||
=={{header|Axe}}== |
=={{header|Axe}}== |
||
< |
<syntaxhighlight lang="axe">Lbl STRCAT |
||
Copy(r₂,r₁+length(r₁),length(r₂)+1) |
Copy(r₂,r₁+length(r₁),length(r₂)+1) |
||
r₁ |
r₁ |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="basic">S$ = "Hello" |
||
S$ = S$ + " World!" |
S$ = S$ + " World!" |
||
PRINT S$</ |
PRINT S$</syntaxhighlight> |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
A$ = "Hello" |
A$ = "Hello" |
||
A$ = A$ & " World!" |
A$ = A$ & " World!" |
||
PRINT A$ |
PRINT A$ |
||
</ |
</syntaxhighlight> |
||
==={{header|BASIC256}}=== |
|||
<syntaxhighlight lang="freebasic">a$ = "He" |
|||
a$ = a$ & "llo" |
|||
a$ = a$ + " Wo" |
|||
a$ += "rld" |
|||
a$ &= "!" |
|||
print a$</syntaxhighlight> |
|||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
< |
<syntaxhighlight lang="bbc basic"> S$="Hello" |
||
S$+=" World!" |
S$+=" World!" |
||
PRINT S$ |
PRINT S$ |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello World!</pre> |
<pre>Hello World!</pre> |
||
==={{header|Commodore BASIC}}=== |
|||
<syntaxhighlight lang="gwbasic">10 S$ = "HELLO" |
|||
20 S$ = S$ + " WORLD!" |
|||
30 PRINT S$ |
|||
40 END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>HELLO WORLD!</pre> |
|||
==={{header|Chipmunk Basic}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
<syntaxhighlight lang="qbasic">10 a$ = "Hello" |
|||
20 a$ = a$ + " World!" |
|||
30 print a$</syntaxhighlight> |
|||
==={{header|GW-BASIC}}=== |
|||
{{works with|PC-BASIC|any}} |
|||
{{works with|BASICA}} |
|||
{{works with|Applesoft Basic}} |
|||
{{works with|Chipmunk Basic}} |
|||
{{works with|QBasic}} |
|||
{{works with|Quite BASIC}} |
|||
{{works with|MSX BASIC}} |
|||
<syntaxhighlight lang="qbasic">10 LET a$ = "Hello" |
|||
20 LET a$ = a$ + " World!" |
|||
30 PRINT a$</syntaxhighlight> |
|||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 LET S$="Hello" |
||
110 LET S$=S$&" World!" |
110 LET S$=S$&" World!" |
||
120 PRINT S$</ |
120 PRINT S$</syntaxhighlight> |
||
==={{header|Liberty BASIC}}=== |
|||
<syntaxhighlight lang="lb">a$ = "Hello" |
|||
a$ = a$ + " World" |
|||
a$ = a$ ; "!" |
|||
print a$</syntaxhighlight> |
|||
==={{header|MSX Basic}}=== |
|||
The [[#GW-BASIC|GW-BASIC]] solution works without any changes. |
|||
==={{header|QBasic}}=== |
|||
{{works with|Liberty BASIC}} |
|||
{{works with|QB64}} |
|||
{{works with|Run BASIC}} |
|||
{{works with|Yabasic}} |
|||
<syntaxhighlight lang="qbasic">a$ = "Hello" |
|||
a$ = a$ + " World!" |
|||
PRINT a$</syntaxhighlight> |
|||
==={{header|Quite BASIC}}=== |
|||
The [[#GW-BASIC|GW-BASIC]] solution works without any changes. |
|||
==={{header|Run BASIC}}=== |
|||
{{works with|Liberty BASIC}} |
|||
{{works with|QB64}} |
|||
{{works with|QBasic}} |
|||
{{works with|Yabasic}} |
|||
<syntaxhighlight lang="runbasic">a$ = "Hello" |
|||
a$ = a$ + " World!" |
|||
print a$</syntaxhighlight> |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">LET a$ = "Hello" |
|||
LET a$ = a$ & " World!" |
|||
PRINT a$ |
|||
END</syntaxhighlight> |
|||
==={{header|uBasic/4tH}}=== |
|||
<syntaxhighlight lang="qbasic">a := "Hello" |
|||
a = Join (a, " World!") |
|||
Print Show(a)</syntaxhighlight> |
|||
==={{header|XBasic}}=== |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="qbasic">PROGRAM "String append" |
|||
VERSION "0.0000" |
|||
DECLARE FUNCTION Entry () |
|||
FUNCTION Entry () |
|||
a$ = "Hello" |
|||
a$ = a$ + " World!" |
|||
PRINT a$ |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
==={{header|Yabasic}}=== |
|||
{{works with|Liberty BASIC}} |
|||
{{works with|QB64}} |
|||
{{works with|QBasic}} |
|||
{{works with|Run BASIC}} |
|||
<syntaxhighlight lang="yabasic">a$ = "Hello" |
|||
a$ = a$ + " World!" |
|||
print a$</syntaxhighlight> |
|||
=={{header|Binary Lambda Calculus}}== |
|||
BLC program |
|||
<pre>18 16 46 80 05 bc bc fd f6 e0 69 6f 6e</pre> |
|||
based on https://github.com/tromp/AIT/blob/master/rosetta/catstrings.lam |
|||
appends "ion" to "on" to output "onion". |
|||
=={{header|BQN}}== |
|||
<syntaxhighlight lang="bqn">str ← "oneTwo" |
|||
•Out str ∾↩ "Three" |
|||
str</syntaxhighlight> |
|||
{{out}} |
|||
<pre>oneTwoThree |
|||
"oneTwoThree"</pre> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">str="Hello"; |
||
str$(!str " World!"):?str; |
str$(!str " World!"):?str; |
||
out$!str;</ |
out$!str;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello World!</pre> |
<pre>Hello World!</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include<stdio.h> |
||
#include<string.h> |
#include<string.h> |
||
Line 373: | Line 558: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Good Morning to all !!!</pre> |
<pre>Good Morning to all !!!</pre> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">class Program |
||
{ |
{ |
||
static void Main(string[] args) |
static void Main(string[] args) |
||
Line 386: | Line 571: | ||
System.Console.WriteLine(x); |
System.Console.WriteLine(x); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 397: | Line 582: | ||
std::cout << greeting << std::endl ; |
std::cout << greeting << std::endl ; |
||
return 0 ; |
return 0 ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello , world!</pre> |
<pre>Hello , world!</pre> |
||
Line 403: | Line 588: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Using global vars. |
Using global vars. |
||
< |
<syntaxhighlight lang="clojure">user=> (def s "app") |
||
#'user/s |
#'user/s |
||
user=> s |
user=> s |
||
Line 410: | Line 595: | ||
#'user/s |
#'user/s |
||
user=> s |
user=> s |
||
"append"</ |
"append"</syntaxhighlight> |
||
Using local bindings. |
Using local bindings. |
||
< |
<syntaxhighlight lang="clojure"> |
||
user=> (let [s "ap", s (str s "pend")] s) |
user=> (let [s "ap", s (str s "pend")] s) |
||
"append"</ |
"append"</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Line 423: | Line 608: | ||
{{works with|GnuCOBOL}} |
{{works with|GnuCOBOL}} |
||
< |
<syntaxhighlight lang="cobol"> identification division. |
||
program-id. string-append. |
program-id. string-append. |
||
Line 446: | Line 631: | ||
goback. |
goback. |
||
end program string-append. |
end program string-append. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>$ cobc -xj string-append.cob |
<pre>$ cobc -xj string-append.cob |
||
Line 453: | Line 638: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
{{works with|Node.js}} |
{{works with|Node.js}} |
||
< |
<syntaxhighlight lang="coffeescript">a = "Hello, " |
||
b = "World!" |
b = "World!" |
||
c = a + b |
c = a + b |
||
console.log c</ |
console.log c</syntaxhighlight> |
||
Or with concat: |
Or with concat: |
||
< |
<syntaxhighlight lang="coffeescript">console.log "Hello, ".concat "World!"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello, World!</pre> |
<pre>Hello, World!</pre> |
||
Line 465: | Line 650: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Similar to the [[String append#Racket| Racket]] solution, a macro is necessary to append in-place: |
Similar to the [[String append#Racket| Racket]] solution, a macro is necessary to append in-place: |
||
< |
<syntaxhighlight lang="lisp">(defmacro concatenatef (s &rest strs) |
||
"Append additional strings to the first string in-place." |
"Append additional strings to the first string in-place." |
||
`(setf ,s (concatenate 'string ,s ,@strs))) |
`(setf ,s (concatenate 'string ,s ,@strs))) |
||
Line 472: | Line 657: | ||
(format T "~a~%" *str*) |
(format T "~a~%" *str*) |
||
(concatenatef *str* "baz" "abc" "def") |
(concatenatef *str* "baz" "abc" "def") |
||
(format T "~a~%" *str*)</ |
(format T "~a~%" *str*)</syntaxhighlight> |
||
Output: |
Output: |
||
Line 479: | Line 664: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
void main() { |
void main() { |
||
Line 485: | Line 670: | ||
s ~= " world!"; |
s ~= " world!"; |
||
writeln(s); |
writeln(s); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello world!</pre> |
<pre>Hello world!</pre> |
||
=={{header| |
=={{header|Dart}}== |
||
<syntaxhighlight lang="dart">void main() { |
|||
String str = "Hello"; |
|||
str = str + " World!"; |
|||
print(str); |
|||
}</syntaxhighlight> |
|||
=={{header|DBL}}== |
|||
<lang Dyalect>var s = "foo" |
|||
<syntaxhighlight lang="dbl">;Concatenate "Hello world!" |
|||
s += "bar" |
|||
STR='Hello' |
|||
print(s)</lang> |
|||
STR(%TRIM(STR)+2:5)='world' |
|||
STR(%TRIM(STR)+1:1)='!'</syntaxhighlight> |
|||
=={{header|Delphi}}== |
|||
{{libheader| System.SysUtils}} |
|||
<syntaxhighlight lang="delphi"> |
|||
program String_append; |
|||
{$APPTYPE CONSOLE} |
|||
uses |
|||
System.SysUtils; |
|||
type |
|||
TStringHelper = record helper for string |
|||
procedure Append(str: string); |
|||
end; |
|||
{ TStringHelper } |
|||
procedure TStringHelper.Append(str: string); |
|||
begin |
|||
Self := self + str; |
|||
end; |
|||
begin |
|||
var h: string; |
|||
// with + operator |
|||
h := 'Hello'; |
|||
h := h + ' World'; |
|||
writeln(h); |
|||
// with a function concat |
|||
h := 'Hello'; |
|||
h := Concat(h, ' World'); |
|||
writeln(h); |
|||
// with helper |
|||
h := 'Hello'; |
|||
h.Append(' World'); |
|||
writeln(h); |
|||
readln; |
|||
end.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Hello World |
|||
Hello World |
|||
Hello World</pre> |
|||
=={{header|Dyalect}}== |
|||
<syntaxhighlight lang="dyalect">var str = "foo" |
|||
str += str |
|||
print(str)</syntaxhighlight> |
|||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>a$ = "hello" |
<syntaxhighlight lang="text">a$ = "hello" |
||
a$ &= " world" |
a$ &= " world" |
||
print a$</ |
print a$</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
;; Solution from Common Lisp and Racket |
;; Solution from Common Lisp and Racket |
||
(define-syntax-rule (set-append! str tail) |
(define-syntax-rule (set-append! str tail) |
||
Line 511: | Line 755: | ||
name |
name |
||
→ "Albert de Jeumont-Schneidre" |
→ "Albert de Jeumont-Schneidre" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA |
ELENA 6.x : |
||
< |
<syntaxhighlight lang="elena">import extensions'text; |
||
import extensions'text; |
|||
public program() |
public program() |
||
{ |
{ |
||
var s := StringWriter.load("Hello"); |
var s := StringWriter.load("Hello"); |
||
s.append |
s.append(" World"); |
||
console. |
console.writeLine(s).readChar() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">iex(60)> s = "Hello" |
||
"Hello" |
"Hello" |
||
iex(61)> s <> " World!" |
iex(61)> s <> " World!" |
||
"Hello World!"</ |
"Hello World!"</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
===version 1=== |
|||
While strings in Emacs Lisp are mutable, they're fixed size. Therefore the <code>concat</code> function creates a new string and the existing string must be referenced twice: |
|||
<lang Emacs Lisp> |
|||
(defun glue (str1 str2) |
|||
<syntaxhighlight lang="lisp">(defvar str "foo") |
|||
(concat str1 str2) ) |
|||
(setq str (concat str "bar")) |
|||
</lang> |
|||
str ;=> "foobar"</syntaxhighlight> |
|||
===version 2=== |
|||
<lang Emacs Lisp> |
|||
This can be hidden by using a macro such as <code>cl-callf</code> which expands into the above code: |
|||
(defun glue (str1 str2) |
|||
(format "%s%s" str1 str2) ) |
|||
{{libheader|cl-lib}} |
|||
</lang> |
|||
<syntaxhighlight lang="lisp">(require 'cl-lib) |
|||
<b>Eval:</b> |
|||
<lang Emacs Lisp> |
|||
( |
(defvar str "foo") |
||
( |
(cl-callf concat str "bar") |
||
str ;=> "foobar"</syntaxhighlight> |
|||
(insert str) |
|||
</lang> |
|||
Buffers can be thought of as expandable strings: |
|||
<b>Output:</b> |
|||
<syntaxhighlight lang="lisp">(let ((buf (get-buffer-create "*foo*"))) |
|||
(with-current-buffer buf |
|||
(insert "foo")) |
|||
(with-current-buffer buf |
|||
(goto-char (point-max)) |
|||
(insert "bar") |
|||
(buffer-string))) |
|||
;; => "foobar"</syntaxhighlight> |
|||
=={{header|EMal}}== |
|||
<syntaxhighlight lang="emal"> |
|||
^|EMal has mutable strings; |
|||
|the append method changes the value in-place. |
|||
|^ |
|||
text hello = "Hello, " |
|||
hello.append("world!") |
|||
writeLine(hello) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
<pre> |
||
Hello, |
Hello, world! |
||
</pre> |
</pre> |
||
Line 564: | Line 827: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria"> |
||
sequence string = "String" |
sequence string = "String" |
||
Line 572: | Line 835: | ||
printf(1,"%s",{string}) |
printf(1,"%s",{string}) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 581: | Line 844: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Strings are immutable in .NET. To append (to the same variable) the variable has to be declared mutable. |
Strings are immutable in .NET. To append (to the same variable) the variable has to be declared mutable. |
||
< |
<syntaxhighlight lang="fsharp">let mutable x = "foo" |
||
x <- x + "bar" |
x <- x + "bar" |
||
printfn "%s" x</ |
printfn "%s" x</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">"Hello, " "world!" append</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 593: | Line 856: | ||
=={{header|Falcon}}== |
=={{header|Falcon}}== |
||
< |
<syntaxhighlight lang="falcon"> |
||
/* Added by Aykayayciti Earl Lamont Montgomery |
/* Added by Aykayayciti Earl Lamont Montgomery |
||
April 10th, 2018 */ |
April 10th, 2018 */ |
||
Line 600: | Line 863: | ||
> s1 + " World" |
> s1 + " World" |
||
printl(s2 + " bar") |
printl(s2 + " bar") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 609: | Line 872: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">\ Strings in Forth are simply named memory locations |
||
create astring 256 allot \ create a "string" |
create astring 256 allot \ create a "string" |
||
Line 615: | Line 878: | ||
s" Hello " astring PLACE \ initialize the string |
s" Hello " astring PLACE \ initialize the string |
||
s" World!" astring +PLACE \ append with "+place"</ |
s" World!" astring +PLACE \ append with "+place"</syntaxhighlight> |
||
Test at the console |
Test at the console |
||
<lang> ok |
<syntaxhighlight lang="text"> ok |
||
s" Hello " astring place ok |
s" Hello " astring place ok |
||
s" World!" astring +place ok |
s" World!" astring +place ok |
||
astring count type Hello World! ok |
astring count type Hello World! ok |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 629: | Line 892: | ||
'''Using deferred length character strings:''' |
'''Using deferred length character strings:''' |
||
<syntaxhighlight lang="fortran"> |
|||
<lang Fortran> |
|||
program main |
program main |
||
Line 640: | Line 903: | ||
end program main |
end program main |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 648: | Line 911: | ||
'''Using pre-allocated character strings:''' |
'''Using pre-allocated character strings:''' |
||
<syntaxhighlight lang="fortran"> |
|||
<lang Fortran> |
|||
program str_append |
program str_append |
||
implicit none |
implicit none |
||
Line 659: | Line 922: | ||
end program str_append |
end program str_append |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 665: | Line 928: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Var s = "String" |
Var s = "String" |
||
s += " append" |
s += " append" |
||
Print s |
Print s |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 676: | Line 939: | ||
String append |
String append |
||
</pre> |
</pre> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
Str255 s |
|||
s = "Hello" |
|||
s += " World!" |
|||
print s |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=0b17e205d56985c8cd1ff108c6fc9ca4 Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=0b17e205d56985c8cd1ff108c6fc9ca4 Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim sString As String = "Hello " |
Dim sString As String = "Hello " |
||
Line 685: | Line 960: | ||
Print sString |
Print sString |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Hello World! |
Hello World! |
||
</pre> |
|||
=={{header|GDScript}}== |
|||
{{works with|Godot|4.0.1}} |
|||
<syntaxhighlight lang="gdscript"> |
|||
extends MainLoop |
|||
func _process(_delta: float) -> bool: |
|||
var string: String = "123" |
|||
string += "abc" |
|||
print(string) |
|||
return true # Exit |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
123abc |
|||
</pre> |
</pre> |
||
=={{header|Genie}}== |
=={{header|Genie}}== |
||
< |
<syntaxhighlight lang="genie">[indent=4] |
||
/* String append, in Genie */ |
/* String append, in Genie */ |
||
init |
init |
||
Line 698: | Line 992: | ||
str += ", world" |
str += ", world" |
||
print str</ |
print str</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>prompt$ valac stringAppend.gs |
<pre>prompt$ valac stringAppend.gs |
||
Line 705: | Line 999: | ||
=={{header|GlovePIE}}== |
=={{header|GlovePIE}}== |
||
< |
<syntaxhighlight lang="glovepie">var.string="This is " |
||
var.string+="Sparta!" |
var.string+="Sparta!" |
||
debug=var.string</ |
debug=var.string</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">s := "foo" |
||
s += "bar"</ |
s += "bar"</syntaxhighlight> |
||
=== String Builder === |
=== 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. |
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. |
||
<syntaxhighlight lang="go"> |
|||
<lang go> |
|||
package main |
package main |
||
Line 729: | Line 1,023: | ||
fmt.Print(s.String()) |
fmt.Print(s.String()) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
foobar |
foobar |
||
=={{header|Gosu}}== |
=={{header|Gosu}}== |
||
< |
<syntaxhighlight lang="gosu">// Example 1 |
||
var s = "a" |
var s = "a" |
||
s += "b" |
s += "b" |
||
Line 747: | Line 1,041: | ||
var b = "b" |
var b = "b" |
||
var c = "c" |
var c = "c" |
||
print("${a}${b}${c}")</ |
print("${a}${b}${c}")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 756: | Line 1,050: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
<syntaxhighlight lang="groovy"> |
|||
<lang Groovy> |
|||
class Append{ |
class Append{ |
||
static void main(String[] args){ |
static void main(String[] args){ |
||
Line 765: | Line 1,059: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 773: | Line 1,067: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell"> |
||
main = putStrLn ("Hello" ++ "World") |
main = putStrLn ("Hello" ++ "World") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
In both languages you can: |
In both languages you can: |
||
< |
<syntaxhighlight lang="unicon"> |
||
procedure main() |
procedure main() |
||
s := "foo" |
s := "foo" |
||
Line 786: | Line 1,080: | ||
write(s) |
write(s) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
Outputs: |
Outputs: |
||
Line 797: | Line 1,091: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j"> s=: 'new' |
||
s |
s |
||
new |
new |
||
s=: s,' value' NB. append is in-place |
s=: s,' value' NB. append is in-place |
||
s |
s |
||
new value</ |
new value</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
There are multiple ways to append string values in Java.<br /> |
|||
<lang Java>String sa = "Hello"; |
|||
The most common way is through the plus operator. |
|||
<syntaxhighlight lang="java"> |
|||
String string = "abc" + "def"; |
|||
</syntaxhighlight> |
|||
Which can also be written as |
|||
<syntaxhighlight lang="java"> |
|||
String string = "abc"; |
|||
string += "def"; |
|||
</syntaxhighlight> |
|||
There is also the ''String.concat'' method |
|||
<syntaxhighlight lang="java"> |
|||
String string = "abc".concat("def"); |
|||
</syntaxhighlight> |
|||
You could use a ''StringBuilder'' object if you're appending multiple strings. |
|||
<syntaxhighlight lang="java"> |
|||
StringBuilder string = new StringBuilder(); |
|||
string.append("abc").append("def"); |
|||
</syntaxhighlight> |
|||
''StringBuilder'' also conveniently lets you insert strings within strings.<br /> |
|||
So, you can also append a string as follows |
|||
<syntaxhighlight lang="java"> |
|||
StringBuilder string = new StringBuilder(); |
|||
string.append("abc"); |
|||
string.insert(3, "def"); |
|||
</syntaxhighlight> |
|||
A less common approach would be to use the ''String.format'' or ''String.formatted'' methods. |
|||
<syntaxhighlight lang="java"> |
|||
String string = String.format("%s%s", "abc", "def"); |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="java"> |
|||
String string = "%s%s".formatted("abc", "def"); |
|||
</syntaxhighlight> |
|||
All of these methods will produce the following string |
|||
<pre> |
|||
abcdef |
|||
</pre> |
|||
<br /> |
|||
Alternately |
|||
<syntaxhighlight lang="java">String sa = "Hello"; |
|||
sa += ", World!"; |
sa += ", World!"; |
||
System.out.println(sa); |
System.out.println(sa); |
||
Line 812: | Line 1,145: | ||
ba.append("Hello"); |
ba.append("Hello"); |
||
ba.append(", World!"); |
ba.append(", World!"); |
||
System.out.println(ba.toString());</ |
System.out.println(ba.toString());</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 822: | Line 1,155: | ||
{{works with|Rhino}} |
{{works with|Rhino}} |
||
{{works with|SpiderMonkey}} |
{{works with|SpiderMonkey}} |
||
< |
<syntaxhighlight lang="javascript">var s1 = "Hello"; |
||
s1 += ", World!"; |
s1 += ", World!"; |
||
print(s1); |
print(s1); |
||
Line 829: | Line 1,162: | ||
// concat() returns the strings together, but doesn't edit existing string |
// concat() returns the strings together, but doesn't edit existing string |
||
// concat can also have multiple parameters |
// concat can also have multiple parameters |
||
print(s2.concat(", World!"));</ |
print(s2.concat(", World!"));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 837: | Line 1,170: | ||
=={{header|jq}}== |
=={{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:< |
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:<syntaxhighlight lang="jq">"Hello" | . += ", world!" |
||
["Hello"] | .[0] += ", world!" | .[0] |
["Hello"] | .[0] += ", world!" | .[0] |
||
{ "greeting": "Hello"} | .greeting += ", world!" | .greeting</ |
{ "greeting": "Hello"} | .greeting += ", world!" | .greeting</syntaxhighlight> |
||
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:< |
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:<syntaxhighlight lang="jq">"Hello" as $a | $a | . += ", world!" as $a | $a</syntaxhighlight> |
||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
From Javascript entry. |
From Javascript entry. |
||
< |
<syntaxhighlight lang="javascript">/* String append, in Jsish */ |
||
var str = 'Hello'; |
var str = 'Hello'; |
||
;str += ', world'; |
;str += ', world'; |
||
Line 858: | Line 1,191: | ||
s2.concat(', World!') ==> Goodbye, World! |
s2.concat(', World!') ==> Goodbye, World! |
||
=!EXPECTEND!= |
=!EXPECTEND!= |
||
*/</ |
*/</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 866: | Line 1,199: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">s = "Hello" |
||
s *= ", world!"</ |
s *= ", world!"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"Hello, world!"</pre> |
<pre>"Hello, world!"</pre> |
||
=={{header|K}}== |
|||
<syntaxhighlight lang=K>h: "hello " |
|||
h,: "world" |
|||
h |
|||
"hello world"</syntaxhighlight> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="kotlin">fun main(args: Array<String>) { |
||
var s = "a" |
var s = "a" |
||
s += "b" |
s += "b" |
||
Line 882: | Line 1,220: | ||
val c = "c" |
val c = "c" |
||
println("$a$b$c") |
println("$a$b$c") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>abc |
<pre>abc |
||
Line 890: | Line 1,228: | ||
=={{header|Lambdatalk}}== |
=={{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. |
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. |
||
<syntaxhighlight lang="scheme"> |
|||
<lang Scheme> |
|||
{def christian_name Albert} |
{def christian_name Albert} |
||
-> christian_name |
-> christian_name |
||
Line 898: | Line 1,236: | ||
{christian_name} {name} |
{christian_name} {name} |
||
-> Albert de Jeumont-Schneidre |
-> Albert de Jeumont-Schneidre |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lang}}== |
|||
<syntaxhighlight lang="lang"> |
|||
$s = Hello |
|||
$s += \, World! |
|||
fn.println($s) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Hello World |
|||
</pre> |
|||
=={{header|langur}}== |
=={{header|langur}}== |
||
< |
<syntaxhighlight lang="langur">var s = "no more " |
||
s ~= "foo bars" |
|||
writeln |
writeln s</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 909: | Line 1,259: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(x = 'Hello') |
||
#x->append(', World!') |
#x->append(', World!') |
||
#x</ |
#x</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello, World!</pre> |
<pre>Hello, World!</pre> |
||
=={{header|LDPL}}== |
|||
<syntaxhighlight lang="ldpl">data: |
|||
myText is text |
|||
procedure: |
|||
store "LD" in myText |
|||
in myText join myText "PL" |
|||
display myText lf |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
LDPL |
|||
</pre> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">str = "Hello" |
||
put " world!" after str |
put " world!" after str |
||
put str |
put str |
||
-- "Hello world!"</ |
-- "Hello world!"</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
Livecode has an "after" keyword for this |
Livecode has an "after" keyword for this |
||
< |
<syntaxhighlight lang="livecode">local str="live" |
||
put "code" after str</ |
put "code" after str</syntaxhighlight> |
||
Output is "livecode" |
Output is "livecode" |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Not possible as strings are immutable. We can demonstrate their immutability using 'self': |
Not possible as strings are immutable. We can demonstrate their immutability using 'self': |
||
< |
<syntaxhighlight lang="lua">function string:show () |
||
print(self) |
print(self) |
||
end |
end |
||
Line 940: | Line 1,304: | ||
x:show() |
x:show() |
||
x:append("there!") |
x:append("there!") |
||
x:show()</ |
x:show()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hi |
<pre>Hi |
||
Hi </pre> |
Hi </pre> |
||
You can of course concatentate them and store the result in the original variable name but that requires a double reference: |
You can of course concatentate them and store the result in the original variable name but that requires a double reference: |
||
< |
<syntaxhighlight lang="lua">x = "Hi " |
||
x = x .. "there!" |
x = x .. "there!" |
||
print(x)</ |
print(x)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hi there!</pre> |
<pre>Hi there!</pre> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Documents in M2000 are objects with paragraphs. |
Documents in M2000 are objects with paragraphs. From Version 12 we can use variable names for strings without suffix $, although a and a$ are different variable names. |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
a="ok" |
|||
a+="(one)" |
|||
Print a |
|||
<lang M2000 Interpreter> |
|||
a$="ok" |
a$="ok" |
||
a$+="(one)" |
a$+="(one)" |
||
Line 963: | Line 1,331: | ||
b$="(one)" |
b$="(one)" |
||
Print b$ |
Print b$ |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>ok(one) |
<pre>ok(one) |
||
Line 970: | Line 1,338: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">a := "Hello"; |
||
b := cat(a, " World"); |
b := cat(a, " World"); |
||
c := `||`(a, " World");</ |
c := `||`(a, " World");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 980: | Line 1,348: | ||
</pre> |
</pre> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<syntaxhighlight lang="mathematica">(* mutable strings are not supported *) |
|||
<lang Mathematica> |
|||
(* mutable strings are not supported *) |
|||
s1 = "testing"; |
s1 = "testing"; |
||
s1 = s1 <> " 123"; |
s1 = s1 <> " 123"; |
||
s1</ |
s1</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"testing 123"</pre> |
<pre>"testing 123"</pre> |
||
Line 991: | Line 1,358: | ||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.6}} |
{{works with|min|0.19.6}} |
||
< |
<syntaxhighlight lang="min">(quote cons "" join) :str-append |
||
"foo" "bar" str-append puts!</ |
"foo" "bar" str-append puts!</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
foobar |
foobar |
||
</pre> |
|||
=={{header|MiniScript}}== |
|||
<syntaxhighlight lang="miniscript"> |
|||
s1 = "Hello" |
|||
s1 = s1 + ", " |
|||
s1 += "World!" |
|||
print s1 |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Hello, World! |
|||
</pre> |
</pre> |
||
=={{header|MontiLang}}== |
=={{header|MontiLang}}== |
||
< |
<syntaxhighlight lang="montilang">|Hello | |world!| swap + print</syntaxhighlight> |
||
< |
<syntaxhighlight lang="montilang">|Hello | var hello . |
||
|world!| var world . |
|world!| var world . |
||
world hello + print</ |
world hello + print</syntaxhighlight> |
||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang="nanoquery">s1 = "this is" |
||
s1 += " a test" |
s1 += " a test" |
||
println s1</ |
println s1</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,017: | Line 1,396: | ||
The plus operator +, concats strings. |
The plus operator +, concats strings. |
||
<syntaxhighlight lang="actionscript">/** |
|||
<lang ActionScript>/** |
|||
<doc><p>String append in Neko</pre></doc> |
<doc><p>String append in Neko</pre></doc> |
||
**/ |
**/ |
||
Line 1,023: | Line 1,402: | ||
var str = "Hello" |
var str = "Hello" |
||
str += ", world" |
str += ", world" |
||
$print(str, "\n")</ |
$print(str, "\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>prompt$ nekoc string-append.neko |
<pre>prompt$ nekoc string-append.neko |
||
Line 1,030: | Line 1,409: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">s_ = 'Hello' |
||
s_ = s_', world!' |
s_ = s_', world!' |
||
say s_</ |
say s_</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,039: | Line 1,418: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(setq str "foo") |
||
(push "bar" str -1) |
(push "bar" str -1) |
||
Line 1,046: | Line 1,425: | ||
(println str) |
(println str) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">var str = "123456" |
||
str.add("78") # |
str.add("78") # Using procedure "add". |
||
str &= "9!" # |
str &= "9!" # Using operator "&=". |
||
echo str |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>123456789!</pre> |
|||
=={{header|NS-HUBASIC}}== |
=={{header|NS-HUBASIC}}== |
||
< |
<syntaxhighlight lang="ns-hubasic">10 S$ = "HELLO" |
||
20 S$ = S$ + " WORLD!" |
20 S$ = S$ + " WORLD!" |
||
30 PRINT S$</ |
30 PRINT S$</syntaxhighlight> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
class Append { |
class Append { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 1,067: | Line 1,451: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let () = |
||
let s = Buffer.create 17 in |
let s = Buffer.create 17 in |
||
Buffer.add_string s "Bonjour"; |
Buffer.add_string s "Bonjour"; |
||
Buffer.add_string s " tout le monde!"; |
Buffer.add_string s " tout le monde!"; |
||
print_endline (Buffer.contents s)</ |
print_endline (Buffer.contents s)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Bonjour tout le monde!</pre> |
<pre>Bonjour tout le monde!</pre> |
||
Line 1,080: | Line 1,464: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">StringBuffer new "Hello, " << "World!" << println</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Not supported in GP. |
Not supported in GP. |
||
< |
<syntaxhighlight lang="parigp">s = "Hello"; |
||
s = Str(s, ", world!")</ |
s = Str(s, ", world!")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = "Hello, world!"</pre> |
<pre>%1 = "Hello, world!"</pre> |
||
Line 1,093: | Line 1,477: | ||
{{works with|Free Pascal|2.6.2}} |
{{works with|Free Pascal|2.6.2}} |
||
< |
<syntaxhighlight lang="pascal">program StringAppend; |
||
{$mode objfpc}{$H+} |
{$mode objfpc}{$H+} |
||
Line 1,109: | Line 1,493: | ||
WriteLn(S); |
WriteLn(S); |
||
ReadLn; |
ReadLn; |
||
end.</ |
end.</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>Hello World !</pre> |
<pre>Hello World !</pre> |
||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
begin |
|||
var s := 'Pascal'; |
|||
s += 'ABC'; |
|||
s += '.NET'; |
|||
s.Println; |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>PascalABC.NET</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">my $str = 'Foo'; |
||
$str .= 'bar'; |
$str .= 'bar'; |
||
print $str;</ |
print $str;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Foobar</pre> |
<pre>Foobar</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
|||
<lang Phix>string s = "this string" ?s |
|||
<!--<syntaxhighlight lang="phix">--> |
|||
s &= " is now longer" ?s</lang> |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"this string"</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">s</span> |
|||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" is now longer"</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">s</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,128: | Line 1,530: | ||
"this string is now longer" |
"this string is now longer" |
||
</pre> |
</pre> |
||
=={{header|Phixmonti}}== |
|||
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/String_append |
|||
by Galileo, 11/2022 #/ |
|||
"Hello" var s |
|||
s " world" chain var s |
|||
s print |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Hello world |
|||
=== Press any key to exit ===</pre> |
|||
=={{header|Picat}}== |
|||
<syntaxhighlight lang="picat">main => |
|||
S = "a string", |
|||
S := S + " that is longer", |
|||
println(S).</syntaxhighlight> |
|||
{{out}} |
|||
<pre>a string that is longer</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(setq Str1 "12345678") |
||
(setq Str1 (pack Str1 "9!")) |
(setq Str1 (pack Str1 "9!")) |
||
(println Str1)</ |
(println Str1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"123456789!"</pre> |
<pre>"123456789!"</pre> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
<syntaxhighlight lang="pike"> |
|||
<lang Pike> |
|||
string msg = "hello"; |
string msg = "hello"; |
||
msg += " world"; |
msg += " world"; |
||
write(msg +"\n"); |
write(msg +"\n"); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,148: | Line 1,572: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">Cat: procedure options (main); |
||
declare s character (100) varying; |
declare s character (100) varying; |
||
s = 'dust '; |
s = 'dust '; |
||
s ||= 'bowl'; |
s ||= 'bowl'; |
||
put (s); |
put (s); |
||
end Cat;</ |
end Cat;</syntaxhighlight> |
||
<pre>dust bowl</pre> |
<pre>dust bowl</pre> |
||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
< |
<syntaxhighlight lang="plainenglish">To run: |
||
Start up. |
Start up. |
||
Put "abc" into a string. |
Put "abc" into a string. |
||
Line 1,163: | Line 1,587: | ||
Write the string to the console. |
Write the string to the console. |
||
Wait for the escape key. |
Wait for the escape key. |
||
Shut down.</ |
Shut down.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,171: | Line 1,595: | ||
=={{header|plainTeX}}== |
=={{header|plainTeX}}== |
||
Works with any TeX engine |
Works with any TeX engine |
||
< |
<syntaxhighlight lang="tex">\def\addtomacro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}} |
||
\def\foo{Hello} |
\def\foo{Hello} |
||
Initial: \foo |
Initial: \foo |
||
Line 1,177: | Line 1,601: | ||
\addtomacro\foo{ world!} |
\addtomacro\foo{ world!} |
||
Appended: \foo |
Appended: \foo |
||
\bye</ |
\bye</syntaxhighlight> |
||
pdf or dvi output: |
pdf or dvi output: |
||
Line 1,185: | Line 1,609: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
$str = "Hello, " |
$str = "Hello, " |
||
$str += "World!" |
$str += "World!" |
||
$str |
$str |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre>Hello, World!</pre> |
<pre>Hello, World!</pre> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">S$ = "Hello" |
||
S$ = S$ + " Wo" ;by referencing the string twice |
S$ = S$ + " Wo" ;by referencing the string twice |
||
S$ + "rld!" ;by referencing the string once |
S$ + "rld!" ;by referencing the string once |
||
Line 1,201: | Line 1,625: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre>Hello World!</pre> |
<pre>Hello World!</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
'''File: String_append.py'''< |
'''File: String_append.py'''<syntaxhighlight lang="python">#!/usr/bin/env python |
||
# -*- coding: utf-8 -*- # |
# -*- coding: utf-8 -*- # |
||
str = "12345678"; |
str = "12345678"; |
||
str += "9!"; |
str += "9!"; |
||
print(str)</ |
print(str)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
123456789! |
123456789! |
||
</pre> |
</pre> |
||
=={{header|QB64}}== |
|||
<syntaxhighlight lang="qbasic">s$ = "String" |
|||
s$ = s$ + " append" |
|||
Print s$</syntaxhighlight> |
|||
{{out}} |
|||
<pre>String append</pre> |
|||
=={{header|Quackery}}== |
|||
Quackery has no variables. The nearest equivalent is ''ancillary stacks''. The word <code>join</code> will return the concatenation of two nests on the stack, and hence two strings, as strings are a particular usage of nests. Here we define the word <code>append</code> to join a nest on the stack to a nest on an ancillary stack, and then demonstrate its use with a Jules Renard quotation and the predefined ancillary stack <code>temp</code>. |
|||
<syntaxhighlight lang="quackery"> [ tuck take swap join swap put ] is append ( [ s --> ) |
|||
$ "L'homme qui attend de voir un canard roti voler " temp put |
|||
$ "dans sa bouche doit attendre tres, tres longtemps." temp append |
|||
temp take echo$ </syntaxhighlight> |
|||
{{out}} |
|||
<pre>L'homme qui attend de voir un canard roti voler dans sa bouche doit attendre tres, tres longtemps.</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">;there is no built-in way to set! append in racket |
||
(define mystr "foo") |
(define mystr "foo") |
||
(set! mystr (string-append mystr " bar")) |
(set! mystr (string-append mystr " bar")) |
||
Line 1,229: | Line 1,674: | ||
(define mymacrostr "foo") |
(define mymacrostr "foo") |
||
(set-append! mymacrostr " bar") |
(set-append! mymacrostr " bar") |
||
(displayln mystr)</ |
(displayln mystr)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,239: | Line 1,684: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>my $str = "foo"; |
||
$str ~= "bar"; |
$str ~= "bar"; |
||
say $str;</ |
say $str;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>foobar</pre> |
<pre>foobar</pre> |
||
=={{header|Relation}}== |
=={{header|Relation}}== |
||
<syntaxhighlight lang="relation"> |
|||
<lang Relation> |
|||
set a = "Hello" |
set a = "Hello" |
||
set b = " World" |
set b = " World" |
||
set c = a.b |
set c = a.b |
||
echo c |
echo c |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===using abutment=== |
===using abutment=== |
||
< |
<syntaxhighlight lang="rexx">s='he' |
||
s=s'llo world!' |
s=s'llo world!' |
||
Say s</ |
Say s</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 1,264: | Line 1,709: | ||
===using concatenation=== |
===using concatenation=== |
||
< |
<syntaxhighlight lang="rexx">s="He" |
||
s=s || 'llo, World!' /*same as: s=s||'llo, World!' */ |
s=s || 'llo, World!' /*same as: s=s||'llo, World!' */ |
||
say s</ |
say s</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 1,273: | Line 1,718: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
aString1 = "Welcome to the " |
aString1 = "Welcome to the " |
||
aString2 = "Ring Programming Language" |
aString2 = "Ring Programming Language" |
||
aString3 = aString1 + aString2 |
aString3 = aString1 + aString2 |
||
see aString3 |
see aString3 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Robotic}}== |
=={{header|Robotic}}== |
||
< |
<syntaxhighlight lang="robotic"> |
||
set "$str1" to "Hello " |
set "$str1" to "Hello " |
||
inc "$str1" by "world!" |
inc "$str1" by "world!" |
||
* "&$str1&" |
* "&$str1&" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|RPL}}== |
|||
In HP-48+ RPL versions, the <code>STO+</code> instruction can append a string to a variable containing already a string. |
|||
"def" '<span style="color:green">Z</span>' STO |
|||
'<span style="color:green">Z</span>' "ghi" STO+ |
|||
<span style="color:green">Z</span> |
|||
'''Output''' |
|||
<span style="color:grey"> 1:</span> "defghi" |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">s = "Hello wo" |
||
s += "rld" # new string object |
s += "rld" # new string object |
||
s << "!" # mutates in place, same object |
s << "!" # mutates in place, same object |
||
puts s</ |
puts s</syntaxhighlight> |
||
{{out}}<pre>Hello world!</pre> |
{{out}}<pre>Hello world!</pre> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
use std::ops::Add; |
use std::ops::Add; |
||
Line 1,302: | Line 1,755: | ||
let hello = String::from("Hello world"); |
let hello = String::from("Hello world"); |
||
println!("{}", hello.add("!!!!")); |
println!("{}", hello.add("!!!!")); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Hello world!!!! |
Hello world!!!! |
||
Line 1,308: | Line 1,761: | ||
=== Real append === |
=== Real append === |
||
The first solution doesn't append to the string variable. This solution really appends to the existing variable. |
The first solution doesn't append to the string variable. This solution really appends to the existing variable. |
||
< |
<syntaxhighlight lang="rust"> |
||
fn main(){ |
fn main(){ |
||
let mut hello = String::from("Hello world"); |
let mut hello = String::from("Hello world"); |
||
Line 1,314: | Line 1,767: | ||
println!("{}", hello); |
println!("{}", hello); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Hello world!!!! |
Hello world!!!! |
||
Line 1,320: | Line 1,773: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
An evaluation in Scala worksheet. |
An evaluation in Scala worksheet. |
||
< |
<syntaxhighlight lang="scala"> var d = "Hello" // Mutables are discouraged //> d : String = Hello |
||
d += ", World!" // var contains a totally new re-instantiationed String |
d += ", World!" // var contains a totally new re-instantiationed String |
||
Line 1,327: | Line 1,780: | ||
val f2 = () => " !" //Function assigned to variable |
val f2 = () => " !" //Function assigned to variable |
||
//> f2 : () => String = <function0> |
//> f2 : () => String = <function0> |
||
println(s1 + f2()); //> HelloHello !</ |
println(s1 + f2()); //> HelloHello !</syntaxhighlight> |
||
=={{header|sed}}== |
|||
There are no variables in ''sed'', just two distinct locations for storing a string: The "pattern space" and the "hold space". |
|||
* The <code>H</code> command appends the content of pattern space to the hold space. |
|||
* The <code>G</code> command appends the content of hold space to the pattern space. |
|||
* The <code>N</code> command appends the next input line to the pattern space. |
|||
All three commands insert a newline character between both strings during appending. In pattern space, it can be removed afterwards with the <code>s</code> command, like this: |
|||
<syntaxhighlight lang="sed">N |
|||
s/\n//</syntaxhighlight> |
|||
* The <code>s</code> command can also be used to append a string literal to the pattern space: |
|||
<syntaxhighlight lang="sed">s/$/String Literal/</syntaxhighlight> |
|||
* To append further lines to the output (after the printing of the pattern space), the <code>a</code> command is useful: |
|||
<syntaxhighlight lang="sed">a\ |
|||
one more line\ |
|||
and another one</syntaxhighlight> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 1,338: | Line 1,806: | ||
str &:= "9!"; |
str &:= "9!"; |
||
writeln(str); |
writeln(str); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,346: | Line 1,814: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var str = 'Foo'; |
||
str += 'bar'; |
str += 'bar'; |
||
say str;</ |
say str;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Foobar</pre> |
<pre>Foobar</pre> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang="snobol4"> s = "Hello" |
||
s = s ", World!" |
s = s ", World!" |
||
OUTPUT = s |
OUTPUT = s |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Hello, World!</pre> |
<pre>Hello, World!</pre> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">sca s="Ars Longa" |
||
sca s=s+" Vita Brevis" |
sca s=s+" Vita Brevis" |
||
di s |
di s |
||
Ars Longa Vita Brevis</ |
Ars Longa Vita Brevis</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">var s = "foo" // "foo" |
||
s += "bar" // "foobar" |
s += "bar" // "foobar" |
||
print(s) // "foobar" |
print(s) // "foobar" |
||
s.appendContentsOf("baz") // "foobarbaz" |
s.appendContentsOf("baz") // "foobarbaz" |
||
print(s) // "foobarbaz"</ |
print(s) // "foobarbaz"</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{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: |
String concatenation is a fundamental feature of the Tcl language, and there is also an <code>append</code> that makes concatenation even simpler: |
||
< |
<syntaxhighlight lang="tcl">set s "he" |
||
set s "${s}llo wo"; # The braces distinguish varname from text to concatenate |
set s "${s}llo wo"; # The braces distinguish varname from text to concatenate |
||
append s "rld" |
append s "rld" |
||
puts $s</ |
puts $s</syntaxhighlight> |
||
{{out}}<pre>hello world</pre> |
{{out}}<pre>hello world</pre> |
||
=={{header|Transd}}== |
|||
<syntaxhighlight lang="scheme">#lang transd |
|||
MainModule: { |
|||
_start: (λ |
|||
(with s1 "aaa" s2 "bbb" s3 "ccc" |
|||
(+= s1 s2 s3) |
|||
(textout s1)) |
|||
) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
aaabbbccc |
|||
</pre> |
|||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
< |
<syntaxhighlight lang="ursa">decl string str |
||
set str "hello " |
set str "hello " |
||
Line 1,390: | Line 1,873: | ||
# outputs "hello world" |
# outputs "hello world" |
||
out str endl console</ |
out str endl console</syntaxhighlight> |
||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang="vala">void main() { |
||
string x = "foo"; |
string x = "foo"; |
||
x += "bar\n"; |
x += "bar\n"; |
||
print(x); |
print(x); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Function StringAppend() |
||
Dim s As String |
Dim s As String |
||
s = "foo" |
s = "foo" |
||
s = s & "bar" |
s = s & "bar" |
||
Debug.Print s |
Debug.Print s |
||
End Function</ |
End Function</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">s = "Rosetta" |
||
s = s & " Code" |
s = s & " Code" |
||
WScript.StdOut.Write s</ |
WScript.StdOut.Write s</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Rosetta Code</pre> |
<pre>Rosetta Code</pre> |
||
=={{header|V (Vlang)}}== |
|||
<syntaxhighlight lang="v (vlang)">mut s:= 'foo' |
|||
s += 'bar' |
|||
println(s) |
|||
foo := 'foo' |
|||
bar := 'bar' |
|||
foobar := '$foo$bar' |
|||
println(foobar)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>foobar |
|||
foobar |
|||
</pre> |
|||
=== String Builder === |
|||
TODO |
|||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
< |
<syntaxhighlight lang="python">s <- "12345678" |
||
s <- (s + "9!")</ |
s <- (s + "9!")</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="wren">var s = "Hello, " |
||
s = s + "world!" |
s = s + "world!" |
||
System.print(s)</ |
System.print(s)</syntaxhighlight> |
||
{{out}} |
|||
<pre> |
|||
Hello, world! |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang="xpl0">include xpllib; |
|||
char A, B, C(80); |
|||
[A:= "Hello, "; |
|||
B:= "world!"; |
|||
StrCopy(C, A); |
|||
StrCat(C, B); |
|||
Text(0, C); |
|||
]</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
Line 1,430: | Line 1,946: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
zkl strings are immutable, but byte blobs are mutable. |
zkl strings are immutable, but byte blobs are mutable. |
||
< |
<syntaxhighlight lang="zkl">var s="foo"; |
||
s.append("bar"); //-->new string "foobar", var s unchanged |
s.append("bar"); //-->new string "foobar", var s unchanged |
||
s+="bar"; //-->new string "foobar", var s modifed to new value |
s+="bar"; //-->new string "foobar", var s modifed to new value |
||
Line 1,436: | Line 1,952: | ||
s=Data(Void,"foo"); // byte blob/character blob/text editor buffer |
s=Data(Void,"foo"); // byte blob/character blob/text editor buffer |
||
s.append("bar"); // or s+="bar" |
s.append("bar"); // or s+="bar" |
||
s.text; //-->"foobar"</ |
s.text; //-->"foobar"</syntaxhighlight> |
||
{{omit from|bc|No string operations in bc}} |
{{omit from|bc|No string operations in bc}} |
Latest revision as of 21:19, 14 June 2024
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.
- 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.
11l
V s = ‘12345678’
s ‘’= ‘9!’
print(s)
- Output:
123456789!
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"
- Output:
String : Alphabet : String : Alphabet : abcdefghijklmnopqrstuvwxyz
Action!
CHAR ARRAY S1,S2
Proc Main()
S1="Hello, "
S2="world!";
Sassign(S1,S2,S1(0)+1)
Print(S1)
Return
- Output:
Hello, world!
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;
- Output:
Hello, world!
ALGOL 68
File: String_append.a68
#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
STRING str := "12345678";
str +:= "9!";
print(str)
- Output:
123456789!
Amazing Hopper
#include <jambo.h>
Main
r="un corderito", s="María tenía", t="felpudo"
Multicat ( s," ",r," ",t ),Get utf8, and print it
d=0
Let ' d := Utf8( Cat( Cat ( s, " un " ), t ) )'
Printnl ( "\n",d )
End
- Output:
María tenía un corderito felpudo María tenía un felpudo
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
AppleScript
set {a, b} to {"Apple", "Script"}
set a to a & b
return a as string
- Output:
"AppleScript"
APL
s←'hello'
s,'world'
helloworld
Arturo
print join ["Hello" "World"]
a: new "Hello"
'a ++ "World"
print a
b: new "Hello"
append 'b "World"
print b
c: "Hello"
print append c "World"
- Output:
HelloWorld HelloWorld HelloWorld HelloWorld
Asymptote
string s = "Hello";
s = s + " Wo";
s += "rld!";
write(s);
AutoHotkey
s := "Hello, "
s .= "world."
MsgBox % s
- Output:
Hello, world.
Avail
Avail's normal strings are immutable, however string variables can leverage tuple's appending-assignment method, _↑++=_
.
str : string := "99 bottles of ";
str ++= "beer";
Print: str;
Note that one can define methods similar to this, thanks to the variable occurrence message pattern, _↑
, 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:
Public method "_↑++=_" is
[
var : read tuple/write ⊥,
t : tuple
|
var ?= eject var ++ t;
] : ⊤;
(eject
and ?=
are methods used for unassign-retrieving and assigning to a variable, respectively, only needed when dealing with the containers themselves.)
AWK
# syntax: GAWK -f STRING_APPEND.AWK
BEGIN {
s = "foo"
s = s "bar"
print(s)
exit(0)
}
- Output:
foobar
Axe
Lbl STRCAT
Copy(r₂,r₁+length(r₁),length(r₂)+1)
r₁
Return
BASIC
Applesoft BASIC
S$ = "Hello"
S$ = S$ + " World!"
PRINT S$
BaCon
A$ = "Hello"
A$ = A$ & " World!"
PRINT A$
BASIC256
a$ = "He"
a$ = a$ & "llo"
a$ = a$ + " Wo"
a$ += "rld"
a$ &= "!"
print a$
BBC BASIC
S$="Hello"
S$+=" World!"
PRINT S$
END
- Output:
Hello World!
Commodore BASIC
10 S$ = "HELLO"
20 S$ = S$ + " WORLD!"
30 PRINT S$
40 END
- Output:
HELLO WORLD!
Chipmunk Basic
10 a$ = "Hello"
20 a$ = a$ + " World!"
30 print a$
GW-BASIC
10 LET a$ = "Hello"
20 LET a$ = a$ + " World!"
30 PRINT a$
IS-BASIC
100 LET S$="Hello"
110 LET S$=S$&" World!"
120 PRINT S$
Liberty BASIC
a$ = "Hello"
a$ = a$ + " World"
a$ = a$ ; "!"
print a$
MSX Basic
The GW-BASIC solution works without any changes.
QBasic
a$ = "Hello"
a$ = a$ + " World!"
PRINT a$
Quite BASIC
The GW-BASIC solution works without any changes.
Run BASIC
a$ = "Hello"
a$ = a$ + " World!"
print a$
True BASIC
LET a$ = "Hello"
LET a$ = a$ & " World!"
PRINT a$
END
uBasic/4tH
a := "Hello"
a = Join (a, " World!")
Print Show(a)
XBasic
PROGRAM "String append"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
a$ = "Hello"
a$ = a$ + " World!"
PRINT a$
END FUNCTION
END PROGRAM
Yabasic
a$ = "Hello"
a$ = a$ + " World!"
print a$
Binary Lambda Calculus
BLC program
18 16 46 80 05 bc bc fd f6 e0 69 6f 6e
based on https://github.com/tromp/AIT/blob/master/rosetta/catstrings.lam appends "ion" to "on" to output "onion".
BQN
str ← "oneTwo"
•Out str ∾↩ "Three"
str
- Output:
oneTwoThree "oneTwoThree"
Bracmat
str="Hello";
str$(!str " World!"):?str;
out$!str;
- Output:
Hello World!
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;
}
- Output:
Good Morning to all !!!
C#
class Program
{
static void Main(string[] args)
{
string x = "foo";
x += "bar";
System.Console.WriteLine(x);
}
}
C++
#include <iostream>
#include <string>
int main( ) {
std::string greeting( "Hello" ) ;
greeting.append( " , world!" ) ;
std::cout << greeting << std::endl ;
return 0 ;
}
- Output:
Hello , world!
Clojure
Using global vars.
user=> (def s "app")
#'user/s
user=> s
"app"
user=> (def s (str s "end"))
#'user/s
user=> s
"append"
Using local bindings.
user=> (let [s "ap", s (str s "pend")] s)
"append"
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.
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.
- Output:
$ cobc -xj string-append.cob Hello, world
CoffeeScript
a = "Hello, "
b = "World!"
c = a + b
console.log c
Or with concat:
console.log "Hello, ".concat "World!"
- Output:
Hello, World!
Common Lisp
Similar to the Racket solution, a macro is necessary to append in-place:
(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*)
Output:
foobar foobarbazabcdef
D
import std.stdio;
void main() {
string s = "Hello";
s ~= " world!";
writeln(s);
}
- Output:
Hello world!
Dart
void main() {
String str = "Hello";
str = str + " World!";
print(str);
}
DBL
;Concatenate "Hello world!"
STR='Hello'
STR(%TRIM(STR)+2:5)='world'
STR(%TRIM(STR)+1:1)='!'
Delphi
program String_append;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TStringHelper = record helper for string
procedure Append(str: string);
end;
{ TStringHelper }
procedure TStringHelper.Append(str: string);
begin
Self := self + str;
end;
begin
var h: string;
// with + operator
h := 'Hello';
h := h + ' World';
writeln(h);
// with a function concat
h := 'Hello';
h := Concat(h, ' World');
writeln(h);
// with helper
h := 'Hello';
h.Append(' World');
writeln(h);
readln;
end.
- Output:
Hello World Hello World Hello World
Dyalect
var str = "foo"
str += str
print(str)
EasyLang
a$ = "hello"
a$ &= " world"
print a$
EchoLisp
;; 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"
Elena
ELENA 6.x :
import extensions'text;
public program()
{
var s := StringWriter.load("Hello");
s.append(" World");
console.writeLine(s).readChar()
}
Elixir
iex(60)> s = "Hello"
"Hello"
iex(61)> s <> " World!"
"Hello World!"
Emacs Lisp
While strings in Emacs Lisp are mutable, they're fixed size. Therefore the concat
function creates a new string and the existing string must be referenced twice:
(defvar str "foo")
(setq str (concat str "bar"))
str ;=> "foobar"
This can be hidden by using a macro such as cl-callf
which expands into the above code:
(require 'cl-lib)
(defvar str "foo")
(cl-callf concat str "bar")
str ;=> "foobar"
Buffers can be thought of as expandable strings:
(let ((buf (get-buffer-create "*foo*")))
(with-current-buffer buf
(insert "foo"))
(with-current-buffer buf
(goto-char (point-max))
(insert "bar")
(buffer-string)))
;; => "foobar"
EMal
^|EMal has mutable strings;
|the append method changes the value in-place.
|^
text hello = "Hello, "
hello.append("world!")
writeLine(hello)
- Output:
Hello, world!
Erlang
- Output:
1> S = "Hello". "Hello" 2> S ++ " world". "Hello world"
Euphoria
sequence string = "String"
printf(1,"%s\n",{string})
string &= " is now longer\n"
printf(1,"%s",{string})
- Output:
String String is now longer
F#
Strings are immutable in .NET. To append (to the same variable) the variable has to be declared mutable.
let mutable x = "foo"
x <- x + "bar"
printfn "%s" x
Factor
"Hello, " "world!" append
- Output:
"Hello, world!"
Falcon
/* Added by Aykayayciti Earl Lamont Montgomery
April 10th, 2018 */
s1, s2 = "Hello", "Foo"
> s1 + " World"
printl(s2 + " bar")
- Output:
Hello World Foo bar [Finished in 0.2s]
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"
Test at the console
ok
s" Hello " astring place ok
s" World!" astring +place ok
astring count type Hello World! ok
Fortran
Using deferred length character strings:
program main
character(len=:),allocatable :: str
str = 'hello'
str = str//' world'
write(*,*) str
end program main
- Output:
hello world
Using pre-allocated character strings:
program str_append
implicit none
character(len=20) :: str
str= 'String'
str(len_trim(str)+1:) = 'Append'
print *, str
end program str_append
- Output:
StringAppend
FreeBASIC
' FB 1.05.0 Win64
Var s = "String"
s += " append"
Print s
Sleep
- Output:
String append
FutureBasic
Str255 s
s = "Hello"
s += " World!"
print s
HandleEvents
Gambas
Click this link to run this code
Public Sub Main()
Dim sString As String = "Hello "
sString &= "World!"
Print sString
End
Output:
Hello World!
GDScript
extends MainLoop
func _process(_delta: float) -> bool:
var string: String = "123"
string += "abc"
print(string)
return true # Exit
- Output:
123abc
Genie
[indent=4]
/* String append, in Genie */
init
str:string = "Hello"
str += ", world"
print str
- Output:
prompt$ valac stringAppend.gs prompt$ ./stringAppend Hello, world
GlovePIE
var.string="This is "
var.string+="Sparta!"
debug=var.string
Go
s := "foo"
s += "bar"
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.
package main
import (
"fmt"
"strings"
)
func main() {
var s strings.Builder
s.WriteString("foo")
s.WriteString("bar")
fmt.Print(s.String())
}
- Output:
foobar
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}")
- Output:
abc abc abc
Groovy
class Append{
static void main(String[] args){
def c="Hello ";
def d="world";
def e=c+d;
println(e);
}
}
- Output:
Hello world
Haskell
main = putStrLn ("Hello" ++ "World")
Icon and Unicon
In both languages you can:
procedure main()
s := "foo"
s ||:= "bar"
write(s)
end
Outputs:
->ss foobar ->
J
s=: 'new'
s
new
s=: s,' value' NB. append is in-place
s
new value
Java
There are multiple ways to append string values in Java.
The most common way is through the plus operator.
String string = "abc" + "def";
Which can also be written as
String string = "abc";
string += "def";
There is also the String.concat method
String string = "abc".concat("def");
You could use a StringBuilder object if you're appending multiple strings.
StringBuilder string = new StringBuilder();
string.append("abc").append("def");
StringBuilder also conveniently lets you insert strings within strings.
So, you can also append a string as follows
StringBuilder string = new StringBuilder();
string.append("abc");
string.insert(3, "def");
A less common approach would be to use the String.format or String.formatted methods.
String string = String.format("%s%s", "abc", "def");
String string = "%s%s".formatted("abc", "def");
All of these methods will produce the following string
abcdef
Alternately
String sa = "Hello";
sa += ", World!";
System.out.println(sa);
StringBuilder ba = new StringBuilder();
ba.append("Hello");
ba.append(", World!");
System.out.println(ba.toString());
- Output:
Hello, World! Hello, World!
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!"));
- Output:
"Hello, World!" "Goodbye, World!"
jq
jq's +
operator can be used to append two strings, and under certain circumstances the +=
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:
"Hello" | . += ", world!"
["Hello"] | .[0] += ", world!" | .[0]
{ "greeting": "Hello"} | .greeting += ", world!" | .greeting
However the +=
operator cannot be used with jq variables in the conventional manner. One could nevertheless use the technique illustrated by the following:
"Hello" as $a | $a | . += ", world!" as $a | $a
Jsish
From Javascript entry.
/* 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!=
*/
- Output:
prompt$ jsish --U stringAppend.jsi str += ', world' ==> Hello, world s2.concat(', World!') ==> Goodbye, World!
Julia
s = "Hello"
s *= ", world!"
- Output:
"Hello, world!"
K
h: "hello "
h,: "world"
h
"hello world"
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")
}
- Output:
abc abc abc
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.
{def christian_name Albert}
-> christian_name
{def name de Jeumont-Schneidre}
-> name
{christian_name} {name}
-> Albert de Jeumont-Schneidre
Lang
$s = Hello
$s += \, World!
fn.println($s)
- Output:
Hello World
langur
var s = "no more "
s ~= "foo bars"
writeln s
- Output:
no more foo bars
Lasso
local(x = 'Hello')
#x->append(', World!')
#x
- Output:
Hello, World!
LDPL
data:
myText is text
procedure:
store "LD" in myText
in myText join myText "PL"
display myText lf
- Output:
LDPL
Lingo
str = "Hello"
put " world!" after str
put str
-- "Hello world!"
LiveCode
Livecode has an "after" keyword for this
local str="live"
put "code" after str
Output is "livecode"
Lua
Not possible as strings are immutable. We can demonstrate their immutability using 'self':
function string:show ()
print(self)
end
function string:append (s)
self = self .. s
end
x = "Hi "
x:show()
x:append("there!")
x:show()
- Output:
Hi Hi
You can of course concatentate them and store the result in the original variable name but that requires a double reference:
x = "Hi "
x = x .. "there!"
print(x)
- Output:
Hi there!
M2000 Interpreter
Documents in M2000 are objects with paragraphs. From Version 12 we can use variable names for strings without suffix $, although a and a$ are different variable names.
a="ok"
a+="(one)"
Print a
a$="ok"
a$+="(one)"
Print a$
Document b$
b$="ok"
b$="(one)"
Print b$
- Output:
ok(one) ok(one)
Maple
a := "Hello";
b := cat(a, " World");
c := `||`(a, " World");
- Output:
"Hello" "Hello World" "Hello World"
Mathematica /Wolfram Language
(* mutable strings are not supported *)
s1 = "testing";
s1 = s1 <> " 123";
s1
- Output:
"testing 123"
min
(quote cons "" join) :str-append
"foo" "bar" str-append puts!
- Output:
foobar
MiniScript
s1 = "Hello"
s1 = s1 + ", "
s1 += "World!"
print s1
- Output:
Hello, World!
MontiLang
|Hello | |world!| swap + print
|Hello | var hello .
|world!| var world .
world hello + print
Nanoquery
s1 = "this is"
s1 += " a test"
println s1
- Output:
this is a test
Neko
The plus operator +, concats strings.
/**
<doc><p>String append in Neko</pre></doc>
**/
var str = "Hello"
str += ", world"
$print(str, "\n")
- Output:
prompt$ nekoc string-append.neko prompt$ neko ./string-append.n Hello, world
NetRexx
s_ = 'Hello'
s_ = s_', world!'
say s_
- Output:
Hello, world!
NewLISP
(setq str "foo")
(push "bar" str -1)
; or as an alternative introduced in v.10.1
(extend str "bar")
(println str)
Nim
var str = "123456"
str.add("78") # Using procedure "add".
str &= "9!" # Using operator "&=".
echo str
- Output:
123456789!
NS-HUBASIC
10 S$ = "HELLO"
20 S$ = S$ + " WORLD!"
30 PRINT S$
Objeck
class Append {
function : Main(args : String[]) ~ Nil {
x := "foo";
x->Append("bar");
x->PrintLine();
}
}
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)
- Output:
Bonjour tout le monde!
Oforth
StringBuffer new "Hello, " << "World!" << println
PARI/GP
Not supported in GP.
s = "Hello";
s = Str(s, ", world!")
- Output:
%1 = "Hello, world!"
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.
Output:
Hello World !
PascalABC.NET
begin
var s := 'Pascal';
s += 'ABC';
s += '.NET';
s.Println;
end.
- Output:
PascalABC.NET
Perl
my $str = 'Foo';
$str .= 'bar';
print $str;
- Output:
Foobar
Phix
string s = "this string" ?s s &= " is now longer" ?s
- Output:
"this string" "this string is now longer"
Phixmonti
/# Rosetta Code problem: https://rosettacode.org/wiki/String_append
by Galileo, 11/2022 #/
"Hello" var s
s " world" chain var s
s print
- Output:
Hello world === Press any key to exit ===
Picat
main =>
S = "a string",
S := S + " that is longer",
println(S).
- Output:
a string that is longer
PicoLisp
(setq Str1 "12345678")
(setq Str1 (pack Str1 "9!"))
(println Str1)
- Output:
"123456789!"
Pike
string msg = "hello";
msg += " world";
write(msg +"\n");
- Output:
hello world
PL/I
Cat: procedure options (main);
declare s character (100) varying;
s = 'dust ';
s ||= 'bowl';
put (s);
end Cat;
dust bowl
Plain English
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.
- Output:
abc123
Plain TeX
Works with any TeX engine
\def\addtomacro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\foo{Hello}
Initial: \foo
\addtomacro\foo{ world!}
Appended: \foo
\bye
pdf or dvi output:
Initial: Hello Appended: Hello world!
PowerShell
$str = "Hello, "
$str += "World!"
$str
Hello, World!
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
Sample output:
Hello World!
Python
File: String_append.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- #
str = "12345678";
str += "9!";
print(str)
- Output:
123456789!
QB64
s$ = "String"
s$ = s$ + " append"
Print s$
- Output:
String append
Quackery
Quackery has no variables. The nearest equivalent is ancillary stacks. The word join
will return the concatenation of two nests on the stack, and hence two strings, as strings are a particular usage of nests. Here we define the word append
to join a nest on the stack to a nest on an ancillary stack, and then demonstrate its use with a Jules Renard quotation and the predefined ancillary stack temp
.
[ tuck take swap join swap put ] is append ( [ s --> )
$ "L'homme qui attend de voir un canard roti voler " temp put
$ "dans sa bouche doit attendre tres, tres longtemps." temp append
temp take echo$
- Output:
L'homme qui attend de voir un canard roti voler dans sa bouche doit attendre tres, tres longtemps.
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)
- Output:
foo bar foo bar
Raku
(formerly Perl 6)
my $str = "foo";
$str ~= "bar";
say $str;
- Output:
foobar
Relation
set a = "Hello"
set b = " World"
set c = a.b
echo c
REXX
using abutment
s='he'
s=s'llo world!'
Say s
output
hello world!
using concatenation
s="He"
s=s || 'llo, World!' /*same as: s=s||'llo, World!' */
say s
output
Hello, World!
Ring
aString1 = "Welcome to the "
aString2 = "Ring Programming Language"
aString3 = aString1 + aString2
see aString3
Robotic
set "$str1" to "Hello "
inc "$str1" by "world!"
* "&$str1&"
end
RPL
In HP-48+ RPL versions, the STO+
instruction can append a string to a variable containing already a string.
"def" 'Z' STO 'Z' "ghi" STO+ Z
Output
1: "defghi"
Ruby
s = "Hello wo"
s += "rld" # new string object
s << "!" # mutates in place, same object
puts s
- Output:
Hello world!
Rust
use std::ops::Add;
fn main(){
let hello = String::from("Hello world");
println!("{}", hello.add("!!!!"));
}
- Output:
Hello world!!!!
Real append
The first solution doesn't append to the string variable. This solution really appends to the existing variable.
fn main(){
let mut hello = String::from("Hello world");
hello.push_str("!!!!");
println!("{}", hello);
}
- Output:
Hello world!!!!
Scala
An evaluation in Scala worksheet.
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 !
sed
There are no variables in sed, just two distinct locations for storing a string: The "pattern space" and the "hold space".
- The
H
command appends the content of pattern space to the hold space. - The
G
command appends the content of hold space to the pattern space. - The
N
command appends the next input line to the pattern space.
All three commands insert a newline character between both strings during appending. In pattern space, it can be removed afterwards with the s
command, like this:
N
s/\n//
- The
s
command can also be used to append a string literal to the pattern space:
s/$/String Literal/
- To append further lines to the output (after the printing of the pattern space), the
a
command is useful:
a\
one more line\
and another one
Seed7
$ include "seed7_05.s7i";
const proc: main is func
local
var string: str is "12345678";
begin
str &:= "9!";
writeln(str);
end func;
- Output:
123456789!
Sidef
var str = 'Foo';
str += 'bar';
say str;
- Output:
Foobar
SNOBOL4
s = "Hello"
s = s ", World!"
OUTPUT = s
END
- Output:
Hello, World!
Stata
sca s="Ars Longa"
sca s=s+" Vita Brevis"
di s
Ars Longa Vita Brevis
Swift
var s = "foo" // "foo"
s += "bar" // "foobar"
print(s) // "foobar"
s.appendContentsOf("baz") // "foobarbaz"
print(s) // "foobarbaz"
Tcl
String concatenation is a fundamental feature of the Tcl language, and there is also an append
that makes concatenation even simpler:
set s "he"
set s "${s}llo wo"; # The braces distinguish varname from text to concatenate
append s "rld"
puts $s
- Output:
hello world
Transd
#lang transd
MainModule: {
_start: (λ
(with s1 "aaa" s2 "bbb" s3 "ccc"
(+= s1 s2 s3)
(textout s1))
)
}
- Output:
aaabbbccc
Ursa
decl string str
set str "hello "
# append "world" to str
set str (+ str "world")
# outputs "hello world"
out str endl console
Vala
void main() {
string x = "foo";
x += "bar\n";
print(x);
}
VBA
Function StringAppend()
Dim s As String
s = "foo"
s = s & "bar"
Debug.Print s
End Function
VBScript
s = "Rosetta"
s = s & " Code"
WScript.StdOut.Write s
- Output:
Rosetta Code
V (Vlang)
mut s:= 'foo'
s += 'bar'
println(s)
foo := 'foo'
bar := 'bar'
foobar := '$foo$bar'
println(foobar)
- Output:
foobar foobar
String Builder
TODO
Wart
s <- "12345678"
s <- (s + "9!")
Wren
var s = "Hello, "
s = s + "world!"
System.print(s)
- Output:
Hello, world!
XPL0
include xpllib;
char A, B, C(80);
[A:= "Hello, ";
B:= "world!";
StrCopy(C, A);
StrCat(C, B);
Text(0, C);
]
- Output:
Hello, world!
zkl
zkl strings are immutable, but byte blobs are mutable.
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"
- Programming Tasks
- Basic language learning
- String manipulation
- Basic Data Operations
- Simple
- 11l
- AArch64 Assembly
- Action!
- Ada
- ALGOL 68
- Amazing Hopper
- ARM Assembly
- AppleScript
- APL
- Arturo
- Asymptote
- AutoHotkey
- Avail
- AWK
- Axe
- BASIC
- Applesoft BASIC
- BaCon
- BASIC256
- BBC BASIC
- Commodore BASIC
- Chipmunk Basic
- GW-BASIC
- IS-BASIC
- Liberty BASIC
- MSX Basic
- QBasic
- Quite BASIC
- Run BASIC
- True BASIC
- UBasic/4tH
- XBasic
- Yabasic
- Binary Lambda Calculus
- BQN
- Bracmat
- C
- C sharp
- C++
- Clojure
- COBOL
- CoffeeScript
- Common Lisp
- D
- Dart
- DBL
- Delphi
- System.SysUtils
- Dyalect
- EasyLang
- EchoLisp
- Elena
- Elixir
- Emacs Lisp
- Cl-lib
- EMal
- Erlang
- Euphoria
- F Sharp
- Factor
- Falcon
- Forth
- Fortran
- FreeBASIC
- FutureBasic
- Gambas
- GDScript
- Genie
- GlovePIE
- Go
- Gosu
- Groovy
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Jq
- Jsish
- Julia
- K
- Kotlin
- Lambdatalk
- Lang
- Langur
- Lasso
- LDPL
- Lingo
- LiveCode
- Lua
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- Min
- MiniScript
- MontiLang
- Nanoquery
- Neko
- NetRexx
- NewLISP
- Nim
- NS-HUBASIC
- Objeck
- OCaml
- Oforth
- PARI/GP
- Pascal
- PascalABC.NET
- Perl
- Phix
- Phix/basics
- Phixmonti
- Picat
- PicoLisp
- Pike
- PL/I
- Plain English
- PlainTeX
- PowerShell
- PureBasic
- Python
- QB64
- Quackery
- Racket
- Raku
- Relation
- REXX
- Ring
- Robotic
- RPL
- Ruby
- Rust
- Scala
- Sed
- Seed7
- Sidef
- SNOBOL4
- Stata
- Swift
- Tcl
- Transd
- Ursa
- Vala
- VBA
- VBScript
- V (Vlang)
- Wart
- Wren
- XPL0
- Zkl
- Bc/Omit
- Dc/Omit
- Pages with too many expensive parser function calls