String prepend: Difference between revisions
Added various BASIC dialects (Chipmunk Basic, GW-BASIC, MSX Basic and Quite BASIC)
Thundergnat (talk | contribs) m (→{{header|Quackery}}: fix markup) |
(Added various BASIC dialects (Chipmunk Basic, GW-BASIC, MSX Basic and Quite BASIC)) |
||
(16 intermediate revisions by 13 users not shown) | |||
Line 388:
# and
a$ = "Hello" & a$</syntaxhighlight>
==={{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}}===
Line 393 ⟶ 415:
110 LET S$="Hello"&S$
120 PRINT S$</syntaxhighlight>
==={{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 406 ⟶ 438:
' en RB, LB and BASIC256 would also be valid
a$ = "Hello"; a$</syntaxhighlight>
==={{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}}===
Line 423 ⟶ 465:
a$ = "Hello" + a$
print a$</syntaxhighlight>
=={{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}}==
Line 656 ⟶ 704:
{{out}}
<pre>Hello world!</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
string$ = "Lang"
string$ = "Easy" & string$
print string$
</syntaxhighlight>
{{out}}
<pre>EasyLang</pre>
=={{header|EchoLisp}}==
Line 672 ⟶ 729:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
import extensions'text;
Line 680 ⟶ 737:
var s := "World";
s := "Hello " + s;
console.writeLine
// Alternative way
var s2 := StringWriter.load("World");
s2.insert(0, "Hello ");
console.writeLine
console.readChar()
}</syntaxhighlight>
Line 725 ⟶ 782:
(buffer-string)))
;; => "foobar"</syntaxhighlight>
=={{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 874 ⟶ 947:
writeLn(line)
end.</syntaxhighlight>
=={{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}}==
Line 941 ⟶ 1,034:
=={{header|Java}}==
Java does not have a prepend method.<br />
The most logical way to prepend a string value is with basic concatenation.
<syntaxhighlight lang="java">
String string = "def";
string = "abc" + string;
</syntaxhighlight>
You could also use the ''String.concat'' method.
String string = "def";
string = "abc".concat(string);
</syntaxhighlight>
You could use the ''StringBuilder'' class which provides an ''insert'' method.
<syntaxhighlight lang="java">
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}}==
Line 1,266 ⟶ 1,376:
<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>
- 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}}==
Line 1,516 ⟶ 1,634:
see bString + nl
</syntaxhighlight>
=={{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}}==
Line 1,537 ⟶ 1,663:
val s1 = "Hello" + f2() + s //> s1 : String = Hello, World
println(s1); //> Hello, World</syntaxhighlight>
=={{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}}==
Line 1,575 ⟶ 1,709:
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}}==
Line 1,592 ⟶ 1,765:
=={{header|Swift}}==
{{works with|Swift|5}}
<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)"
Line 1,635 ⟶ 1,817:
<pre>foobar</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">mut s := "world!"
s = "Hello, " + s
println(s)</syntaxhighlight>
Line 1,650 ⟶ 1,832:
=={{header|Wren}}==
<syntaxhighlight lang="
s = "Hello, " + s
System.print(s)</syntaxhighlight>
|