String prepend: Difference between revisions

Added various BASIC dialects (Chipmunk Basic, GW-BASIC, MSX Basic and Quite BASIC)
No edit summary
(Added various BASIC dialects (Chipmunk Basic, GW-BASIC, MSX Basic and Quite BASIC))
 
(8 intermediate revisions by 7 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 681 ⟶ 729:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">import extensions;
import extensions'text;
Line 689 ⟶ 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()
}</syntaxhighlight>
Line 734 ⟶ 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 970 ⟶ 1,034:
 
=={{header|Java}}==
Java does not have a prepend method.<br />
<syntaxhighlight lang="java">// prepend
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">
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}}==
Line 1,553 ⟶ 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,629 ⟶ 1,718:
@( description, "Prepend the string variable with another string " )
@( description, "literal." )
@( description, "" )
@( description, "If your language supports any idiomatic ways to " )
@( description, "do this without referring to the variable twice " )
@( description, "in one expression, include such solutions." )
@( description, "" )
@( description, "To illustrate the operation, show the content of " )
@( description, "the variable." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
Line 1,750 ⟶ 1,832:
 
=={{header|Wren}}==
<syntaxhighlight lang="javascriptwren">var s = "world!"
s = "Hello, " + s
System.print(s)</syntaxhighlight>
2,122

edits