String prepend: Difference between revisions

Added various BASIC dialects (Chipmunk Basic, GW-BASIC, MSX Basic and Quite BASIC)
(simplify →‎Pascal)
(Added various BASIC dialects (Chipmunk Basic, GW-BASIC, MSX Basic and Quite BASIC))
 
(18 intermediate revisions by 14 users not shown)
Line 20:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V s = ‘12345678’
s = ‘0’s
print(s)</langsyntaxhighlight>
 
{{out}}
Line 30:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* String prepend - 14/04/2020
PREPEND CSECT
USING PREPEND,13 base register
Line 47:
B DC C'Hello ' b
C DC CL80' ' c
END PREPEND</langsyntaxhighlight>
{{out}}
<pre>
Line 55:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program appendstr64.s */
Line 170:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 178:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Append(CHAR ARRAY text,suffix)
BYTE POINTER srcPtr,dstPtr
BYTE len
Line 216:
TestPrepend(text,"World!")
TestPrepend(text,"Hello ")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/String_prepend.png Screenshot from Atari 8-bit computer]
Line 233:
In Ada, a variable of type String cannot change its length. So the variable S which we will change, need to be of the type Unbounded_String. Thus the need for conversions from String literal to Unbounded_String for initialization, and from Unbounded_String to String for printing.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
 
procedure Prepend_String is
Line 240:
S := "Hello " & S;-- this is the operation to prepend "Hello " to S.
Ada.Text_IO.Put_Line(To_String(S));
end Prepend_String;</langsyntaxhighlight>
 
{{out}}
Line 250:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.7 algol68g-2.7].}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards).}}
'''File: String_prepend.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
STRING str := "12345678";
"0" +=: str;
print(str)</langsyntaxhighlight>
{{out}}
<pre>
Line 265:
AppleScript text is immutable, so prepending is only possible by creating a new text through concatenation of the variable's existing contents to the other string:
 
<langsyntaxhighlight lang="applescript">set aVariable to "world!"
set aVariable to "Hello " & aVariable
return aVariable</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Hello world!"</langsyntaxhighlight>
 
It's a similar situation with NSString class in AppleScriptObjC. This has various ways of achieving the same thing, probably the most sensible of which is the first of the following:
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
Line 290:
set aVariable to aVariable's stringByReplacingOccurrencesOfString:("^") withString:("Hello ") ¬
options:(current application's NSRegularExpressionSearch) range:({0, 0})
-- return aVariable as text</langsyntaxhighlight>
 
But there's also an NS''Mutable''String class. This has 'replace' versions of the 'stringByReplacing' methods above and also this <tt>insertString:atIndex:</tt> method:
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
set aVariable to current application's class "NSMutableString"'s stringWithString:("world!")
tell aVariable to insertString:("Hello ") atIndex:(0)
return aVariable as text</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Hello world!"</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">a: "World"
a: "Hello" ++ a
print a
Line 318:
 
d: "World"
print prefix d "Hello"</langsyntaxhighlight>
 
{{out}}
Line 328:
 
=={{header|Asymptote}}==
<langsyntaxhighlight Asymptotelang="asymptote">string s1 = " World!";
write("Hello" + s1);
write("Hello", s1);
string s2 = "Hello" + s1;
write(s2);</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">s := "foo"
s := s "bar"
Msgbox % s</langsyntaxhighlight>
{{out}}
<pre>
Line 344:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STRING_PREPEND.AWK
BEGIN {
Line 352:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 359:
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="bacon">s$ = "prepend"
s$ = "String " & s$
PRINT s$</langsyntaxhighlight>
 
{{out}}
Line 367:
 
=={{header|BASIC}}==
<langsyntaxhighlight BBClang="bbc BASICbasic">S$ = " World!"
S$ = "Hello" + S$
PRINT S$
</syntaxhighlight>
</lang>
{{out}}
<pre>Hello World!</pre>
 
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight Applesoftlang="applesoft BASICbasic">100 LET S$=" World!"
110 LET S$="Hello"+S$
120 PRINT S$</langsyntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="basic256">a$ = " World!"
a$ = "Hello"; a$
print a$
Line 387:
a$ = "Hello" + a$
# and
a$ = "Hello" & a$</langsyntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|GW-BASIC}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">10 A$ = " World!"
20 A$ = "Hello" + A$
30 PRINT A$</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">10 A$ = " World!"
20 A$ = "Hello" + A$
30 PRINT A$</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 LET S$=" World!"
110 LET S$="Hello"&S$
120 PRINT S$</langsyntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 A$ = " World!"
20 A$ = "Hello" + A$
30 PRINT A$</syntaxhighlight>
 
==={{header|Run BASIC}}===
Line 400 ⟶ 432:
{{works with|QBasic}}
{{works with|Yabasic}}
<langsyntaxhighlight lang="runbasic">a$ = " World!"
a$ = "Hello" + a$
print a$
 
' en RB, LB and BASIC256 would also be valid
a$ = "Hello"; a$</langsyntaxhighlight>
 
==={{header|Quite BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 LET A$ = " World!"
20 LET A$ = "Hello" + A$
30 PRINT A$</syntaxhighlight>
 
==={{header|True BASIC}}===
{{works with|BASIC256}}
<langsyntaxhighlight lang="qbasic">LET a$ = " World!"
LET a$ = "Hello" & a$
PRINT a$
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
Line 420 ⟶ 462:
{{works with|QBasic}}
{{works with|Run BASIC}}
<langsyntaxhighlight lang="freebasic">a$ = " World!"
a$ = "Hello" + a$
print a$</langsyntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
BLC program
<pre>18 16 46 80 05 bc bc fd f6 e0 67 6d 61</pre>
based on https://github.com/tromp/AIT/blob/master/rosetta/catstrings.lam
prepends "ma" to "gma" to output "magma".
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat"> World!:?string
& str$("Hello " !string):?string
& out$!string</langsyntaxhighlight>
<pre>Hello World!</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<string.h>
#include<stdlib.h>
Line 444 ⟶ 492:
printf("%s\n",str);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Changed my String</pre>
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace PrependString
Line 463 ⟶ 511:
}
}
}</langsyntaxhighlight>
<pre>Hello World</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">include <vector>
#include <algorithm>
#include <string>
Line 479 ⟶ 527:
std::cout << prepended << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>prepended tomy string</pre>
Line 486 ⟶ 534:
 
===A pure function implementation with immutability ===
<langsyntaxhighlight lang="clojure">
(defn str-prepend [a-string, to-prepend]
(str to-prepend a-string))
</syntaxhighlight>
</lang>
 
=== REPL demonstrations with mutability in mind ===
a) with the atom data structure
 
<langsyntaxhighlight lang="clojure">
(def s (atom "World"))
(swap! s #(str "Hello, " %))
Line 500 ⟶ 548:
user=> @s
user=> "Hello, Wolrd"
</syntaxhighlight>
</lang>
 
b) with the ref data structure
<langsyntaxhighlight lang="clojure">
(def s (ref "World"))
(dosync (alter s #(str "Hello " %)))
Line 509 ⟶ 557:
user=> @s
user=> "Hello World"
</syntaxhighlight>
</lang>
 
=={{header|COBOL}}==
<langsyntaxhighlight COBOLlang="cobol"> identification division.
program-id. prepend.
data division.
Line 536 ⟶ 584:
move function reverse (str (1:len)) to str
.
end program prepend.</langsyntaxhighlight>
<pre>Hello World!</pre>
{{works with|GNU Cobol|2.0}}
<langsyntaxhighlight lang="cobol"> >>SOURCE FREE
PROGRAM-ID. prepend.
 
Line 550 ⟶ 598:
DISPLAY str
.
END PROGRAM prepend.</langsyntaxhighlight>
 
=={{header|ColdFusion}}==
=== Classic tag based CFML ===
<langsyntaxhighlight lang="cfm">
<cfoutput>
<cfset who = "World!">
#"Hello " & who#
</cfoutput>
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 566 ⟶ 614:
 
=== Script Based CFML ===
<langsyntaxhighlight lang="cfm"><cfscript>
who = "World!";
greeting = "Hello " & who;
writeOutput( greeting );
</cfscript></langsyntaxhighlight>
{{Output}}
<pre>
Line 578 ⟶ 626:
=={{header|Common Lisp}}==
A macro is necessary in order to prepend a string in-place:
<langsyntaxhighlight lang="lisp">(defmacro prependf (s &rest strs)
"Prepend the given string variable with additional strings. The string variable is modified in-place."
`(setf ,s (concatenate 'string ,@strs ,s)))
Line 584 ⟶ 632:
(defvar *str* "foo")
(prependf *str* "bar")
(format T "~a~%" *str*)</langsyntaxhighlight>
{{out}}
<pre>barfoo</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
void main() {
Line 595 ⟶ 643:
s = "Hello " ~ s;
writeln(s);
}</langsyntaxhighlight>
{{out}}
<pre>Hello world!</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program String_preappend;
 
Line 638 ⟶ 686:
writeln(h);
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Hello World
Line 646 ⟶ 694:
=={{header|Dyalect}}==
 
<langsyntaxhighlight Dyalectlang="dyalect">var s = "world!"
s = "Hello " + s
print(s)</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">local :s "world!"
set :s concat( "Hello " s)
!print s</langsyntaxhighlight>
{{out}}
<pre>Hello world!</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
string$ = "Lang"
string$ = "Easy" & string$
print string$
</syntaxhighlight>
{{out}}
<pre>EasyLang</pre>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
define-syntax-rule
(set!-string-prepend a before)
Line 669 ⟶ 726:
name
→ "Elvis Presley"
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
ELENA 46.x:
<langsyntaxhighlight lang="elena">import extensions;
import extensions'text;
Line 680 ⟶ 737:
var s := "World";
s := "Hello " + s;
console.writeLine:(s);
// Alternative way
var s2 := StringWriter.load("World");
s2.insert(0, "Hello ");
console.writeLine:(s2);
console.readChar()
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
<lang Elixir>
str1 = "World!"
str = "Hello, " <> str1
</syntaxhighlight>
</lang>
 
{{out}}
Line 702 ⟶ 759:
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:
 
<langsyntaxhighlight Lisplang="lisp">(defvar str "bar")
(setq str (concat "foo" str))
str ;=> "foobar"</langsyntaxhighlight>
 
This can be hidden by using a macro such as <code>cl-callf2</code> which expands into the above code:
 
{{libheader|cl-lib}}
<langsyntaxhighlight Lisplang="lisp">(require 'cl-lib)
 
(defvar str "bar")
(cl-callf2 concat "foo" str)
str ;=> "foobar"</langsyntaxhighlight>
 
Buffers can be thought of as expandable strings:
 
<langsyntaxhighlight Lisplang="lisp">(let ((buf (get-buffer-create "*foo*")))
(with-current-buffer buf
(insert "bar"))
Line 724 ⟶ 781:
(insert "foo")
(buffer-string)))
;; => "foobar"</langsyntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
text greeting = "world"
^|basic concatenation|^
writeLine("hello " + greeting)
^|changing the text in place|^
writeLine(greeting.insert(0, "hello "))
writeLine(greeting)
</syntaxhighlight>
{{out}}
<pre>
hello world
hello world
hello world
</pre>
 
=={{header|Erlang}}==
Line 736 ⟶ 809:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
......
S$=" World!"
Line 742 ⟶ 815:
PRINT(S$)
......
</syntaxhighlight>
</lang>
{{out}}
<pre>Hello World!</pre>
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let mutable s = "world!"
s <- "Hello, " + s
printfn "%s" s</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">
"world"
"Hello " prepend
</syntaxhighlight>
</lang>
 
=={{header|Falcon}}==
'''VBA/Python programmer's approach not sure if it's the most falconic way'''
<langsyntaxhighlight lang="falcon">
/* created by Aykayayciti Earl Lamont Montgomery
April 9th, 2018 */
Line 767 ⟶ 840:
s = s + "Falcon"
> s
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 777 ⟶ 850:
Forth has no string prepend word, but adding it is not difficult. This demonstration starts from the low level operations that Forth gives us and quickly builds a simple set of "WORDS" (sub-routines) that let us move strings from address to address. Strings are just an address on the data stack so we can reference them as many times as we need to. Our prepend word makes use of the Return stack as a temporary storage for the address of the string we want to prepend. Standard Forth also provides a named general purpose buffer called PAD, so we make use of that too. With this PREPEND becomes part of the language.
 
<syntaxhighlight lang="text">\ the following functions are commonly native to a Forth system. Shown for completeness
 
: C+! ( n addr -- ) dup c@ rot + swap c! ; \ primitive: increment a byte at addr by n
Line 795 ⟶ 868:
R> ; \ leave a copy of addr2 on the data stack
 
: writeln ( addr -- ) cr count type ; \ syntax sugar for testing</langsyntaxhighlight>
 
Test our language extensions interactively at the console
Line 811 ⟶ 884:
 
===Initial difficulty===
With Fortran IV came the ability to use arrays of integers and the A1 format code in READ and WRITE statements for them. With sixteen-bit integers, one might use A2 and so forth, but the numerical values of the integers would not be convenient especially if the sign bit was involved. This would be even more difficult with floating-point variables. Still, the desire for good headings and annotations and flexible layout flogged one onwards. Following the Pascal "Hello world!" example, one might proceed somewhat as follows:<langsyntaxhighlight Fortranlang="fortran"> INTEGER*4 I,TEXT(66)
DATA TEXT(1),TEXT(2),TEXT(3)/"Wo","rl","d!"/
 
Line 825 ⟶ 898:
WRITE (6,3) (TEXT(I), I = 1,6)
3 FORMAT (66A2)
END</langsyntaxhighlight>
This old-style source is acceptable to the F90 compiler as it stands. By chance, two characters per integer fits nicely but in many cases having one character per variable is easier for manipulation. So, as usual with Fortran, it's all done with arrays. The DATA statement demonstrates that a quoted string is acceptable as a value for an integer; it is just a matter of bit patterns, and this type miscegnation will work with floating-point variables also though resulting in even stranger numerical values. Looking more closely, note that an INTEGER*4 variable can hold four eight-bit characters but only two-character text literals have been specified. Unlike integer constants, which might be considered to have leading zero digits, text literals are deemed to have trailing spaces as needed: <code>"Wo"</code> is deemed to be <code>"Wo "</code> to make up to the recipient's capacity for four characters, and when format code A2 is specified, the leftmost two characters in the variable are taken. The strange ideas of "little-endianism" have never flourished on mainframes! Thus, if the format code were to be A4, then "Wo " would appear, not " Wo".
 
Line 840 ⟶ 913:
 
===Character facility===
With F77 came the CHARACTER type... <langsyntaxhighlight Fortranlang="fortran"> CHARACTER*66 TEXT
TEXT = "World!"
TEXT = "Hello "//TEXT
WRITE (6,*) TEXT
END </langsyntaxhighlight>
This means that variable TEXT has space for 66 characters, addressed as TEXT(''first'':''last'') starting with one. There is no associated string length facility, so the first assignment places the six characters of the supplied literal, followed by spaces all the way to the end of TEXT. Alternatively, <code>TEXT(1:6) = "World!"</code> would place only six characters, leaving the rest of TEXT to hold whatever it may. This would probably be unsuitable for the next statement, which prepends "Hello " to the content of TEXT (including positions past six) and assigns the result to TEXT, overwriting its previous content - with the aid of a temporary working area. Although in principle there could be cunning schemes that update the recipient "in place" with a minimum of character copying to and fro, this doesn't happen. Only characters up to the capacity of the recipient will be transferred from the expression's result, and if the result is shorter than the capacity of the recipient, trailing spaces will be added. All of this is extra effort! And when TEXT is written out, all 66 characters will be sent forth. It is useful to have a function that locates the last non-blank character!
 
Line 851 ⟶ 924:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Var s = "prepend"
s = "String " + s
Print s
Sleep</langsyntaxhighlight>
 
{{out}}
Line 866 ⟶ 939:
Free Pascal supports everything shown in [[#Pascal|§ Pascal]] (except the <tt>string</tt> schema data type, <tt>string(20)</tt> must be written like here).
Furthermore, using the compiler directive <tt>{$COperators}</tt> the following is possible, too:
<langsyntaxhighlight lang="delphi">var
line: string[20];
begin
Line 873 ⟶ 946:
line += 'world!';
writeLn(line)
end.</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
void local fn DoIt
CFStringRef s = @"world!"
s = fn StringByAppendingString( @"Hello ", s )
NSLog(@"%@",s)
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
Hello world!
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=cd5ab867c22e872d69ed81fd9da96707 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sString1 As String = "world!"
Dim sString2 As String = "Hello "
Line 885 ⟶ 978:
Print sString1
 
End</langsyntaxhighlight>
Output:
<pre>
Line 892 ⟶ 985:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">s := "world!"
s = "Hello, " + s</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">
Prelude> let f = (++" World!")
Prelude> f "Hello"
</syntaxhighlight>
</lang>
 
{{out}}
Line 906 ⟶ 999:
=={{header|Icon}} and {{header|Unicon}}==
 
<langsyntaxhighlight lang="unicon">s := "world!"
s := "Hello, " || s</langsyntaxhighlight>
 
To demonstrate how this could be done with only one reference to the variable during the prepend:
 
<langsyntaxhighlight lang="unicon">procedure main()
s := ", world"
s[1:1] ||:= "Hello"
write(s)
end</langsyntaxhighlight>
 
{{out}}
Line 923 ⟶ 1,016:
Another take on it, using String Scanning:
 
<langsyntaxhighlight lang="unicon">procedure main()
(s := ", world") ?:= "Hello" || tab(0)
write(s)
end</langsyntaxhighlight>
 
{{out}}
Line 933 ⟶ 1,026:
 
=={{header|J}}==
<langsyntaxhighlight lang="j"> s=: 'value'
s
value
s=: 'new ',s
s
new value</langsyntaxhighlight>
 
=={{header|Java}}==
<langJava java>//does not have a prepend method.<br />
The most logical way to prepend a string value is with basic concatenation.
public class Prepend {
<syntaxhighlight lang="java">
public static void main(String[] args) {
String string = "def";
StringBuilder sb = new StringBuilder("world");
string = "abc" + string;
sb.insert(0, "Hello, ");
</syntaxhighlight>
System.out.println(sb);
You could also use the ''String.concat'' method.
}
<syntaxhighlight lang="java">
}</lang>
String string = "def";
 
string = "abc".concat(string);
{{out}}
</syntaxhighlight>
<pre>prompt$ javac Prepend.java
You could use the ''StringBuilder'' class which provides an ''insert'' method.
prompt$ java Prepend
<syntaxhighlight lang="java">
Hello, world</pre>
StringBuilder string = new StringBuilder();
string.append("def");
string.insert(0, "abc");
</syntaxhighlight>
Additionally, you could use the ''String.format'' or ''String.formatted'' methods.
<syntaxhighlight lang="java">
String string = "def";
string = String.format("abc%s", string);
</syntaxhighlight>
<syntaxhighlight lang="java">
String string = "def";
string = "abc%s".formatted(string);
</syntaxhighlight>
All of these will produce the following output.
<pre>
abcdef
</pre>
 
=={{header|Javascript}}==
<langsyntaxhighlight lang="javascript">// No built-in prepend
var s=", World"
s = "Hello" + s
print(s);</langsyntaxhighlight>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">"world!" as $s
| "Hello " + $s</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">s = "world!"
s = "Hello " * s</langsyntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
s: "world!"
"world!"
"Hello " , s
"Hello world!"
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun main(args: Array<String>) {
Line 990 ⟶ 1,100:
t = t.prependIndent("Donald ")
println(t)
}</langsyntaxhighlight>
 
{{out}}
Line 999 ⟶ 1,109:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def str World}
-> str
Line 1,005 ⟶ 1,115:
Hello, {str}
-> Hello, World
</syntaxhighlight>
</lang>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">local(x = ', World!')
#x->merge(1,'Hello')
#x // Hello, World!</langsyntaxhighlight>
 
=={{header|LFE}}==
Line 1,016 ⟶ 1,126:
Using the concatenation operator:
 
<langsyntaxhighlight lang="lisp">
> (set s "world")
"world"
> (++ "hello " s)
"hello world"
</syntaxhighlight>
</lang>
 
Using the concatenation function:
 
<langsyntaxhighlight lang="lisp">
> (set s "world")
"world"
> (string:concat "hello " s)
"hello world"
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">str = "world!"
put "Hello " before str
put str
-- "Hello world!"</langsyntaxhighlight>
 
=={{header|LiveCode}}==
The idiomatic way is to use "before"<langsyntaxhighlight LiveCodelang="livecode">put "world" into x
put "hello" before x
put x // hello world</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 1,047 ⟶ 1,157:
By concatenation:
 
<langsyntaxhighlight lang="lua">
s = "12345678"
s = "0" .. s
print(s)</langsyntaxhighlight>
 
By string formatting:
 
<langsyntaxhighlight lang="lua">
s = "12345678"
s = string.format("%s%s", "0", s)
print(s)</langsyntaxhighlight>
 
By list joining:
 
<langsyntaxhighlight lang="lua">
s = "12345678"
s = table.concat({"0", s})
print(s)</langsyntaxhighlight>
 
{{out}} of each solution:
Line 1,072 ⟶ 1,182:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module PrependString {
A$="Hello"
Line 1,079 ⟶ 1,189:
}
PrependString
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,087 ⟶ 1,197:
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">l := " World";
m := cat("Hello", l);
n := "Hello"||l;
o := `||`("Hello", l);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,100 ⟶ 1,210:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">a = "any text value";
a = "another string literal" <> a (* using concatenation (no built-in prepend) *)</langsyntaxhighlight>
{{out}}
<pre>"another string literalany text value"</pre>
 
=={{header|Mercury}}==
<langsyntaxhighlight lang="mercury">:- module string_prepend.
:- interface.
:- import_module io.
Line 1,114 ⟶ 1,224:
main(!IO) :-
S = "World!\n",
io.write_string("Hello " ++ S, !IO).</langsyntaxhighlight>
{{out}}
<pre>
Line 1,122 ⟶ 1,232:
=={{header|Nanoquery}}==
Nanoquery has no idiomatic way to prepend one string to another.
<langsyntaxhighlight Nanoquerylang="nanoquery">s1 = " a test"
s1 = "this is" + s1
 
println s1</langsyntaxhighlight>
 
{{out}}
Line 1,133 ⟶ 1,243:
The plus operator, +, concatenates string data. Neko treats strings as mutable fixed length buffers, so some care would need to be taken when prepending variables to variables as there may be buffer sizing to take into consideration. For literals, this is not a concern, as the literals are placed in buffers of the proper size by the compiler.
 
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
<doc><p>String prepend in Neko</pre></doc>
**/
Line 1,139 ⟶ 1,249:
var str = ", world"
str = "Hello" + str
$print(str, "\n")</langsyntaxhighlight>
{{out}}
<pre>prompt$ nekoc string-prepend.neko
Line 1,146 ⟶ 1,256:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">s_ = 'world!'
s_ = 'Hello, 's_
say s_</langsyntaxhighlight>
{{out}}
<pre>
Line 1,155 ⟶ 1,265:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(setq str "bar")
(push "foo" str)
(println str)</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim"># Direct way.
var str1, str2 = "12345678"
str1 = "0" & str1
Line 1,168 ⟶ 1,278:
str2.insert("0")
echo str2
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,175 ⟶ 1,285:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">class Prepend {
function : Main(args : String[]) ~ Nil {
s := "world!";
"Hello {$s}"->PrintLine();
}
}</langsyntaxhighlight>
 
<pre>
Line 1,187 ⟶ 1,297:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let () =
let s = ", world" in
let s = "Hello" ^ s in
print_endline s</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">" World" "Hello" swap + println</langsyntaxhighlight>
 
{{out}}
Line 1,203 ⟶ 1,313:
=={{header|PARI/GP}}==
Not supported in GP.
<langsyntaxhighlight lang="parigp">s = "world!";
s = Str("Hello, ", s)</langsyntaxhighlight>
{{out}}
<pre>%1 = "Hello, world!"</pre>
Line 1,212 ⟶ 1,322:
{{works with|Extended Pascal}}
 
<langsyntaxhighlight lang="pascal">program stringPrepend(output);
var
line: string(20);
Line 1,223 ⟶ 1,333:
writeStr(line, line, 'world!');
writeLn(line)
end.</langsyntaxhighlight>
{{out}}
<pre>Hello world!
Line 1,229 ⟶ 1,339:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">use strict;
use warnings;
use feature ':all';
Line 1,249 ⟶ 1,359:
$_ = 'bar';
$_ = "Foo$_";
say;</langsyntaxhighlight>
 
{{out}}
Line 1,260 ⟶ 1,370:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"World"</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Hello "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">s</span>
<!--</langsyntaxhighlight>-->
<b>NB:</b> s = prepend(s,"Hello ") gives typecheck: s is {"Hello ",87'W',111'o',114'r',108'l',100'd'}, of length 6, rather than the "Hello World" of length 11 you probably wanted.<br>
&nbsp;&nbsp; &nbsp; &nbsp; - and likewise s = prepend("Hello ",s) is not only the wrong way round but dies with typecheck: s is {"World",72'H',101'e',108'l',108'l',111'o',32' '} (length 7).
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/String_prepend
by Galileo, 10/2022 #/
 
"Hello " var s
s "world" chain var s
s print</syntaxhighlight>
 
=={{header|Picat}}==
As usual there are a couple ways of doing this. The most common is probable to use string concatenation (<code>++</code>), but <code>append/3</code> might be useful if backtracking is needed.
<langsyntaxhighlight Picatlang="picat">go =>
S = "123456789",
Line 1,287 ⟶ 1,405:
S := insert_all(S,1,"DE"),
println(S),
nl.</langsyntaxhighlight>
 
{{out}}
Line 1,297 ⟶ 1,415:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang="picolisp">(setq Str1 "12345678!")
(setq Str1 (pack "0" Str1))
(println Str1)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,306 ⟶ 1,424:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
Pre_Cat: procedure options (main); /* 2 November 2013 */
declare s character (100) varying;
Line 1,313 ⟶ 1,431:
put (s);
end Pre_Cat;
</syntaxhighlight>
</lang>
<pre>
dust bowl
Line 1,319 ⟶ 1,437:
 
=={{header|PlainTeX}}==
<langsyntaxhighlight lang="tex">\def\prepend#1#2{% #1=string #2=macro containing a string
\def\tempstring{#1}%
\expandafter\expandafter\expandafter
Line 1,329 ⟶ 1,447:
\prepend{Hello }\mystring
Result : \mystring
\bye</langsyntaxhighlight>
 
Here is an equivalent code with eTeX capabilities:
<langsyntaxhighlight lang="tex">\def\prepend#1#2{% #1=string #2=macro containing a string
\edef#2{\unexpanded{#1}\unexpanded\expandafter{#2}}%
}
Line 1,338 ⟶ 1,456:
\prepend{Hello }\mystring
Result : \mystring
\bye</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
$str = "World!"
$str = "Hello, " + $str
$str
</syntaxhighlight>
</lang>
<pre>Hello, World!</pre>
 
Line 1,360 ⟶ 1,478:
the string. I define an operator for the purpose:
 
<langsyntaxhighlight lang="prolog">
:- op(200, xfx, user:(=+)).
 
Line 1,373 ⟶ 1,491:
nb_setarg(2, Chars, Rest),
nb_setarg(1, Chars, X).
</syntaxhighlight>
</lang>
 
Example of this abomination in action:
 
<langsyntaxhighlight lang="prolog">
?- Str = `World!`, `Hello, ` =+ Str.
Str = "Hello, World!".</langsyntaxhighlight>
 
Note: I can't imagine when I would want to do this in Prolog.
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">S$ = " World!"
S$ = "Hello" + S$
If OpenConsole()
Line 1,391 ⟶ 1,509:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out}}
<pre>Hello World!</pre>
Line 1,398 ⟶ 1,516:
'''File: string_prepend.py'''
 
<langsyntaxhighlight lang="python">#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
s = "12345678"
s = "0" + s # by concatenation
print(s)</langsyntaxhighlight>
 
{{out}}
Line 1,411 ⟶ 1,529:
 
=={{header|QB64}}==
<langsyntaxhighlight lang="qbasic">s$ = "prepend"
s$ = "String " + s$
Print s$</langsyntaxhighlight>
{{out}}
<pre>String prepend</pre>
Line 1,419 ⟶ 1,537:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery">$ "with a rubber duck."
$ "One is never alone "
swap join
echo$</Langsyntaxhighlight>
 
{{out}}
Line 1,429 ⟶ 1,547:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">;there is no built-in way to set! prepend in racket
(define str "foo")
(set! str (string-append "bar " str))
Line 1,441 ⟶ 1,559:
(set-prepend! macrostr "foo")
(displayln macrostr)
</syntaxhighlight>
</lang>
{{out}}
<pre>bar foo
Line 1,448 ⟶ 1,566:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line># explicit concatentation
$_ = 'byte';
$_ = 'kilo' ~ $_;
Line 1,471 ⟶ 1,589:
$_ = 'cooper';
$_ [R~]= 'mini';
.say;</langsyntaxhighlight>
{{out}}
<pre>kilobyte
Line 1,480 ⟶ 1,598:
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">Red []
s: "world"
insert s "hello "
print s
</syntaxhighlight>
</lang>
{{out}}
<pre>hello world</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">zz= 'llo world!' /*─────────────── using literal abuttal.────────────*/
zz= 'he'zz /*This won't work if the variable name is X or B */
say zz
Line 1,502 ⟶ 1,620:
bString= "he"
aString= bString || aString
say aString</langsyntaxhighlight>
'''output'''
<pre>
Line 1,511 ⟶ 1,629:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
aString = "World!"
bString = "Hello, " + aString
see bString + nl
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
In HP-48+ RPL versions, the <code>STO+</code> instruction can either append or prepend a string to a variable containing already a string.
"def" '<span style="color:green">Z</span>' STO
"abc" '<span style="color:green">Z</span>' STO+
<span style="color:green">Z</span>
'''Output'''
<span style="color:grey"> 1:</span> "abcdef"
 
=={{header|Ruby}}==
There is a method for prepending a string, aptly named "prepend".
<langsyntaxhighlight lang="ruby">str = "llo world"
str.prepend("He")
p str #=> "Hello world"</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
let mut s = "World".to_string();
s.insert_str(0, "Hello ");
println!("{}", s);
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Evaluation in Scala worksheet
<langsyntaxhighlight lang="scala"> val s = "World" // Immutables are recommended //> s : String = World
val f2 = () => ", " //Function assigned to variable
//> f2 : () => String = <function0>
val s1 = "Hello" + f2() + s //> s1 : String = Hello, World
println(s1); //> Hello, World</langsyntaxhighlight>
 
=={{header|sed}}==
There are no variables in ''sed'', just two distinct locations for storing a string: The "pattern space" and the "hold space". To prepend a string literal to the pattern space, the <code>s</code> command can be used:
<syntaxhighlight lang="sed">s/^/String Literal/</syntaxhighlight>
To prepend a string literal to the hold space, it needs to be exchanged with the pattern space, before and after the operation:
<syntaxhighlight lang="sed">x
s/^/String Literal/
x</syntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,547 ⟶ 1,681:
s := "Hello " & s;
writeln(s);
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,555 ⟶ 1,689:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var str = 'llo!';
str.sub!(/^/, 'He');
say str;</langsyntaxhighlight>
 
or
<langsyntaxhighlight lang="ruby">var str = 'llo!';
str.prepend!('He');
say str;</langsyntaxhighlight>
 
{{out}}
Line 1,568 ⟶ 1,702:
 
=={{header|SNOBOL4}}==
<langsyntaxhighlight SNOBOL4lang="snobol4"> s = ', World!'
OUTPUT = s = 'Hello' s
END</langsyntaxhighlight>
{{out}}
<pre>
Hello, World!
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "string_prepend" )
@( description, "Create a string variable equal to any text value." )
@( description, "" )
@( description, "Prepend the string variable with another string " )
@( description, "literal." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/String_prepend" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure string_prepend is
world : constant string := "World!";
hello : constant string := "Hello ";
s : string;
begin
-- Using concatenation
s := world;
s := hello & @;
? s;
 
-- Using strings library
s := world;
s := strings.insert( @, 1, hello );
? s;
 
command_line.set_exit_status( 0 );
end string_prepend;</syntaxhighlight>
{{out}}
<pre>
$ spar string_prepend.sp
Hello World!
Hello World!</pre>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="text">
> val s="any text" ;
val s = "any text": string
> "prepended " ^ s;
val it = "prepended any text": string
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">sca s="Vita Brevis"
sca s="Ars Longa "+s
di s
 
Ars Longa Vita Brevis</langsyntaxhighlight>
 
=={{header|Swift}}==
{{works with|Swift|5}}
<lang Swift>var str = ", World"
<syntaxhighlight lang="swift">var str = ", World"
str = "Hello" + str
print(str)</syntaxhighlight>
{{out}}
<pre>
Hello, World!
</pre>
{{works with|Swift|1}}
<syntaxhighlight lang="swift">var str = ", World"
str = "Hello \(str)"
println(str)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,602 ⟶ 1,784:
=={{header|Tcl}}==
Concatenation is a fundamental feature of Tcl's basic language syntax.
<langsyntaxhighlight lang="tcl">set s "llo world"
set s "he$s"
puts $s</langsyntaxhighlight>
{{out}}
<pre>hello world</pre>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl string s
 
# set s to "world"
Line 1,618 ⟶ 1,800:
 
# outputs "hello world"
out s endl console</langsyntaxhighlight>
 
=={{header|VBA}}==
<langsyntaxhighlight VBlang="vb">Function StringPrepend()
Dim s As String
s = "bar"
s = "foo" & s
Debug.Print s
End Function</langsyntaxhighlight>
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">s = "bar"
s = "foo" & s
WScript.Echo s</langsyntaxhighlight>
{{Out}}
<pre>foobar</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">mut s := "world!"
s = "Hello, " + s
println(s)</langsyntaxhighlight>
 
{{out}}
Line 1,646 ⟶ 1,828:
 
=={{header|Wart}}==
<langsyntaxhighlight lang="python">s <- "12345678"
s <- ("0" + s)</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight javascriptlang="wren">var s = "world!"
s = "Hello, " + s
System.print(s)</langsyntaxhighlight>
 
{{out}}
Line 1,660 ⟶ 1,842:
 
=={{header|Xojo}}==
<langsyntaxhighlight lang="vb">Dim s As String = "bar"
s = "foo" + s
MsgBox(s)</langsyntaxhighlight>
{{Out}}
<pre>foobar</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include xpllib;
char S, T(80);
[S:= "world!";
S:= StrCat(StrCopy(T,"Hello, "), S);
Text(0, S);
]</langsyntaxhighlight>
 
{{out}}
Line 1,680 ⟶ 1,862:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">s:="bar"; s="foo" + s; s.println();
s:="bar"; s=String("foo",s); s.println();
s:="bar"; s="%s%s".fmt("foo",s); s.println();
// a Data is a byte buffer/editor:
s:=Data(Void,"bar").insert(0,"foo").text; s.println();</langsyntaxhighlight>
{{out}}
<pre>
2,122

edits