String interpolation: Difference between revisions

Content deleted Content added
DarthKitty (talk | contribs)
Examples: remove redundant example—Perl and PHP have dedicated sections below
m Examples: lang="output"
Line 32:
DATA(apples) = 4.
WRITE |I have { apples } apples|.
</syntaxhighlight>The output will be:<syntaxhighlight lang="textoutput">
I have 4 apples
</syntaxhighlight>
Line 45:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples</syntaxhighlight>
 
=== Boo ===
Line 56:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples</syntaxhighlight>
 
=== C# ===
Line 69:
 
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples
I have 7 fruits
Line 97:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples</syntaxhighlight>
 
=== Dart ===
Line 107:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples.
I have 7 fruit.</syntaxhighlight>
 
Line 124:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">A developer is a superhero if he is seasoned</syntaxhighlight>
 
=== Haxe ===
Line 135:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples.
I have 7 fruit.</syntaxhighlight><ref>{{Cite news|url=https://haxe.org/manual/lf-string-interpolation.html|title=Haxe - Manual - String interpolation|work=Haxe - The Cross-platform Toolkit|access-date=2017-09-12}}</ref>
 
Line 153:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples
I have 7 fruit
Line 165:
 
The output will be:
<syntaxhighlight lang="textoutput">
This is the first line of text.
This is the second line of text.
Line 178:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples and 3 bananas, making 7 pieces of fruit in total.
</syntaxhighlight>
Line 192:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">A developer is a superhero. I have 7 fruit</syntaxhighlight>
 
=== Nemerle ===
Line 205:
Console.WriteLine($<#I have ..$(fruit; "\n"; f => f + "s")#>);</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">apples
bananas</syntaxhighlight>
 
Line 217:
echo(sentence)</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">A developer is a superhero. I have 7 fruit</syntaxhighlight>
 
=== Nim ===
Line 248:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples
I have 4 apples
Line 267:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples
</syntaxhighlight>
Line 280:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples.
I have 7 fruit.</syntaxhighlight>
Line 293:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples.
I have 7 fruit.</syntaxhighlight>
Line 307:
echo "I have {$apples} apples and {$bananas} bananas.";
</syntaxhighlight>The output will be:
<syntaxhighlight lang="textoutput">There are 5 apples and 3 bananas.
I have 5 apples and 3 bananas.</syntaxhighlight>
 
Line 321:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">
I have 4 apples and 3 bananas</syntaxhighlight>
 
Line 337:
 
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples</syntaxhighlight>
 
=== Rust ===
Line 375:
</syntaxhighlight>
 
The output will be:<syntaxhighlight lang="textoutput">I have 4 apples</syntaxhighlight>
 
=== Sciter (tiscript) ===
Line 390:
</syntaxhighlight>
 
Where <syntaxhighlight lang="textjavascript">domElement.$content(<p>I have {apples} apples</p>);</syntaxhighlight> gets compiled to this:
<syntaxhighlight lang="javascript">domElement.html = "<p>I have " + apples.toHtmlString() + " apples</p>";</syntaxhighlight>
 
Line 401:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples.
I have 7 fruit.</syntaxhighlight>
 
Line 413:
print("I have \(apples) apples")
</syntaxhighlight>The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples</syntaxhighlight>
 
=== Tcl ===
Line 425:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples.</syntaxhighlight>
 
In order to actually format - and not simply replace - the values, there is a formatting function.
Line 443:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="textoutput">I have 4 apples</syntaxhighlight>
The <code>console.log</code> function can be used as a <code>printf</code> function. The above example can be rewritten, thusly:
<syntaxhighlight lang="ecmascript">