String interpolation (included): Difference between revisions

Content deleted Content added
Langurmonkey (talk | contribs)
added langur language example
Aartaka (talk | contribs)
Add ed example
 
(4 intermediate revisions by 4 users not shown)
Line 1,052:
STD.Str.FindReplace('Mary had a X Lamb', 'X','little');
</syntaxhighlight>
 
=={{header|ed}}==
 
<!--</syntaxhighlight>-- lang="sed">
# by Artyom Bologov
H
,p
# Add a vertical bar to separate the template and value
2s/^/|/
,j
# Replace and print
s/X\(.*\)|\(.*\)/\2\1/p
Q
</syntaxhighlight>
 
{{out}}
 
<pre>$ ed -s interpolation.input < interpolation.ed
Newline appended
Mary has a X lamb
big
Mary has a big lamb</pre>
 
=={{header|Elena}}==
Line 1,452 ⟶ 1,474:
All interpolations in langur use double curly braces.
 
<syntaxhighlight lang="langur">val .x = "little"
writeln "Mary had a {{.x}} lamb."</syntaxhighlight>
 
Modifiers follow the interpolated value with a colon.
<syntaxhighlight lang="langur">writeln "Lamb count: {{.x : 7}}"</syntaxhighlight>
 
Modifiers may be chained within a single interpolation.
<syntaxhighlight lang="langur">writeln "Partial lamb count: {{.x : r2 : 10}}"
# rounds to 2 decimal places, then aligns to 10 code points
# (hopefully no partial lambs)
Line 1,663 ⟶ 1,685:
<syntaxhighlight lang="parigp">s=Strprintf("The value was: %Ps", 1<<20);
printf("The value was: %Ps", 1<<20);</syntaxhighlight>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
var extra := 'little';
var formatted := $'Mary has a {extra} lamb.';
Print(formatted);
</syntaxhighlight>
 
 
=={{header|Perl}}==
Line 1,671 ⟶ 1,701:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #004080;">string</span> <span style="color: #000000;">size</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"little"</span>
string size = "little"
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Mary had a %s lamb."</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">size</span><span style="color: #0000FF;">})</span>
string s = sprintf("Mary had a %s lamb.",{size})
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
?s -- or printf(1,".."".."), probably with a \n
<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
Line 2,238 ⟶ 2,269:
Mary had a X lamb.
Mary had a little lamb.
</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.11.1}}
[https://www.uiua.org/tutorial/functions#format-strings Format strings documentation]
<syntaxhighlight lang="uiua">
$"Mary had a _ lamb." "little"
</syntaxhighlight>
{{out}}
<pre>
"Mary had a little lamb."
</pre>