String interpolation (included): Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(13 intermediate revisions by 11 users not shown)
Line 359:
Mary had a little lamb.
</pre>
 
Algol 68 allows a string to be used as a file, the following (based on the above) uses this to construct the string which can then be further processed. In this sample, variable strings and formats are used though in general it would be hard to construct the extraf format at run-time as Algol 68 has no facilities to construct formats on the fly.
 
<syntaxhighlight lang="algol68">
BEGIN
FILE str file;
STRING mhl;
associate( str file, mhl );
 
STRING extra := "little";
TO 2 DO
putf( str file, ( $"Mary had a "g" lamb."$, extra ) );
print( ( "1 result is: {{", mhl, "}}", newline ) );
mhl := "";
"supposedlly-" +=: extra
OD;
 
FORMAT extraf := $"little"$;
TO 2 DO
putf( str file, ( $"Mary had a "f(extraf)" lamb."$ ) );
print( ( "2 result is: {{", mhl, "}}", newline ) );
mhl := "";
extraf := $"medium-sized"$
OD
END
</syntaxhighlight>
 
{{out}}
<pre>
1 result is: {{Mary had a little lamb.}}
1 result is: {{Mary had a supposedlly-little lamb.}}
2 result is: {{Mary had a little lamb.}}
2 result is: {{Mary had a medium-sized lamb.}}
</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
Line 687 ⟶ 722:
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="qbasic">10 x$ = "big"
20 print "Mary had a "; x$; " lamb"</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">x$ = "big"
Line 693 ⟶ 732:
x$ = "little"
print "Mary also had a "; ljust(x$, length(x$)); " lamb"</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 x$ = "big"
20 print "Mary had a "; x$; " lamb"</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">10 X$ = "big"
20 PRINT "Mary had a "; X$; " lamb"
30 X$ = "little"
40 PRINT USING "Mary also had a & lamb"; X$
50 END</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
<syntaxhighlight lang="qbasic">10 LET X$ = "BIG"
20 PRINT "MARY HAD A "; X$; " LAMB"
30 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
Line 701 ⟶ 765:
' this code above doesn't modify the first string subsustituting a piece of it with another string
'surely it gives the right output on the screen</syntaxhighlight>
 
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">10 LET X$ = "BIG"
20 PRINT "Mary had a "; x$; " lamb"</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 712 ⟶ 780:
PRINT "Mary also had a "; outstring$; " lamb"
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "String append"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
X$ = "big"
PRINT "Mary had a "; X$; " lamb"
END FUNCTION
END PROGRAN</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 1,152 ⟶ 1,232:
<syntaxhighlight lang="funl">X = 'little'
println( "Mary had a $X lamb." )</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
CFStringRef string, extra
extra = @"little"
string = fn StringWithFormat( @"Mary had a %@ lamb", extra )
 
print string
 
HandleEvents
</syntaxhighlight>
 
=={{header|Gambas}}==
Line 1,264 ⟶ 1,355:
 
=={{header|Java}}==
Java includes the ''Formatter'' class which includes numerous ways to interpolate text values.<br />
The ''String'' and ''Writer'' classes both implement a form of ''Formatter''.<br /><br />
There are numerous demonstrations on the documentation page on how to apply different interpolations.<br />https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/Formatter.html<br /><br />
The format is inspired by the C ''printf'', and is nearly the same except for a few accomodations.<br />
The most idiomatic approach would be to use the ''String.format'' or ''String.formatted'' methods.
<syntaxhighlight lang="java">
String adjective = "little";
String lyric = String.format("Mary had a %s lamb", adjective);
</syntaxhighlight>
<syntaxhighlight lang="java">
String adjective = "little";
String lyric = "Mary had a %s lamb".formatted(adjective);
</syntaxhighlight>
If you wanted to print the string you could use the ''System.out.printf'' method.
<syntaxhighlight lang="java">
String adjective = "little";
System.out.printf("Mary had a %s lamb", adjective);
</syntaxhighlight>
You could also use a combination of ''StringBuilder'' and ''Formatter''.
<syntaxhighlight lang="java">
StringBuilder string = new StringBuilder();
Formatter formatter = new Formatter(string);
String adjective = "little";
formatter.format("Mary had a %s lamb", adjective);
formatter.flush();
</syntaxhighlight>
<br />
Alternately
<syntaxhighlight lang="java">String original = "Mary had a X lamb";
String little = "little";
Line 1,544 ⟶ 1,663:
"Mary had a little lamb."
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/String_interpolation_(included)
by Galileo, 11/2022 #/
 
include ..\Utilitys.pmt
 
"big" var s
 
( "Mary had a " s " lamb" ) lprint nl
 
"little" var s
 
( "Mary had a " s " lamb" ) lprint
</syntaxhighlight>
{{out}}
<pre>Mary had a big lamb
Mary had a little lamb
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
Line 1,687 ⟶ 1,825:
'Mary had a little lamb.'
>>> </syntaxhighlight>
 
=={{header|Quackery}}==
 
'''The Task'''
 
Quackery does not have built-in string interpolation.
 
'''Not The Task'''
 
<code>interpolate$</code> replaces every instance of a specified character in a string with a specified string.
Note that the specified string should not include the specified character as this will cause <code>interpolate$</code> to loop indefinitely.
 
<syntaxhighlight lang="Quackery"> [ stack ] is int.chr
[ stack ] is int.str
 
[ int.str put
int.chr put
[] swap witheach
[ dup int.chr share = if
[ drop int.str share ]
join ]
int.str release
int.chr release ] is interpolate$ ( $ n $ --> $ )
 
$ "Mary had a lamb." char X $ "little" interpolate$ echo$ cr
$ "Mary had a X lamb." char X $ "little" interpolate$ echo$ cr
$ "Mary had a X X lamb." char X $ "little" interpolate$ echo$ cr</syntaxhighlight>
 
{{out}}
 
<pre>Mary had a lamb.
Mary had a little lamb.
Mary had a little little lamb.
</pre>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
Line 1,800 ⟶ 1,973:
aString =substr("Mary had a X lamb.", "X", "little")
see aString + nl
</syntaxhighlight>
 
An alternate approach using the print() function:
<syntaxhighlight lang="ring">
extra = "little"
print("Mary had a #{extra} lamb.\n")
</syntaxhighlight>
 
Line 2,158 ⟶ 2,337:
 
=={{header|Wren}}==
<syntaxhighlight lang="javascriptwren">var s = "little"
var t = "Mary had a %(s) lamb"
System.print(t)</syntaxhighlight>
Line 2,167 ⟶ 2,346:
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
txt := "little"
str := "Mary had a $txt lamb"
Line 2,188 ⟶ 2,367:
{{omit from|8086 Assembly}}
{{omit from|80386 Assembly}}
{{omit from|Axe}}
{{omit from|bc|No built-in string interpolation in bc}}
{{omit from|dc|No built-in string interpolation in dc}}
{{omit from|GUISS}}
{{omit from|Microsoft Small Basic}}
{{omit from|Unlambda|Does not have built-in string interpolatiopn (or built-in strings, for that matter).}}
{{omit from|Z80 Assembly}}
{{omit from|Axe}}
9,476

edits