Content deleted Content added
→Ruby / Crystal: the latter two are format strings, not interpolation |
Citation bot (talk | contribs) Add: title, date. Changed bare reference to CS1/2. | Use this bot. Report bugs. | Suggested by Folkezoft | Linked from User:Folkezoft/sandbox | #UCB_webform_linked 829/978 |
||
(26 intermediate revisions by 19 users not shown) | |||
Line 1:
{{short description|Replacing placeholders in a string with values}}
In [[computer programming]], '''string interpolation''' (or '''variable interpolation''', '''variable substitution''', or '''variable expansion''') is the process of evaluating a [[string literal]] containing one or more [[Form (document)#Placeholders|placeholders]], yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple [[Template processor|template processing]]<ref>"[
String interpolation is an alternative to building string via [[concatenation]], which requires
<syntaxhighlight lang="
apples = 4
</syntaxhighlight>
Line 66:
Console.WriteLine($"I have {apples} apples");
Console.WriteLine($"I have {apples + bananas} fruits");
</syntaxhighlight><ref>{{Cite web|url=https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#string-interpolation|title = Strings - C# Programming Guide| date=15 March 2024 }}</ref>
The output will be:
Line 101:
=== Dart ===
{{Main|Dart (programming language)}}
<syntaxhighlight lang="
int apples = 4, bananas = 3;
print('I have $apples apples.');
Line 112:
=== Go ===
{{Main|Go (programming language)}}
{{as of|
=== Groovy ===
Line 142:
=== Java ===
{{Main article|Java (programming language)}}
Java had interpolated strings as a preview feature in Java 21 and Java 22. You could use the constant STR of [https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/StringTemplate.html java.lang.StringTemplate] directly.<syntaxhighlight lang="java" line="1">
enum Stage{test,qa,prod}
record Deploy(UUID image, Stage stage){}
var deploy=new Deploy(UUID.randomUUID(), Stage.test)
STR."Installing \{deploy.image()} on Stage \{deploy.stage()} ..."
var deploy=new Deploy(UUID.randomUUID(), Stage.prod)
STR."Installing \{deploy.image()} on Stage \{deploy.stage()} ..."
They were removed in Java 23 due to design issues.<ref>{{cite web | title=Significant Changes in the JDK | url=https://docs.oracle.com/en/java/javase/23/migrate/significant-changes-jdk-release.html }}</ref>
=== JavaScript ===
{{Main article|JavaScript}}
[[JavaScript]], as of the [[ECMAScript]] 2015 (ES6) standard, supports string interpolation using backticks <code>``</code>. This feature is called ''template literals''.<ref>{{Cite web|url=https://developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals|title = Template literals (Template strings) - JavaScript | MDN| date=31 May 2024 }}</ref> Here is an example:▼
<syntaxhighlight lang="javascript">
▲[[JavaScript]], as of the [[ECMAScript]] 2015 (ES6) standard, supports string interpolation using backticks <code>``</code>. This feature is called ''template literals''.<ref>{{Cite web|url=https://developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals|title = Template literals (Template strings) - JavaScript | MDN}}</ref> Here is an example:
▲<syntaxhighlight lang="ecmascript">
const apples = 4;
const bananas = 3;
Line 161 ⟶ 169:
Template literals can also be used for multi-line strings:
<syntaxhighlight lang="
console.log(`This is the first line of text.
This is the second line of text.`);
Line 297 ⟶ 305:
$apples = 5;
$bananas = 3;
echo "There are $apples apples and $bananas bananas.\n";
echo "I have {$apples} apples and {$bananas} bananas.";
</syntaxhighlight>The output will be:
Line 307 ⟶ 314:
{{Main|Python (programming language)}}
Python supports string interpolation as of version 3.6, referred to as
"formatted string literals" or "f-strings".<ref>{{cite web |url=https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings
|title= The Python Tutorial: 7.1.1. Formatted String Literals}}</ref><ref>{{Cite web |url=https://docs.python.org/3/reference/lexical_analysis.html#f-strings |title=The Python Language Reference: 2.4.3. Formatted string literals}}</ref><ref>{{Cite web|url=https://www.python.org/dev/peps/pep-0498/|title = PEP 498 -- Literal String Interpolation}}</ref> Such a literal begins with an <code>f</code> or <code>F</code> before the opening quote, and uses braces for placeholders:
<syntaxhighlight lang="python">
print(f'I have {
</syntaxhighlight>
The output will be:
Line 345 ⟶ 352:
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="output">
</syntaxhighlight>
=== Scala ===
{{Main article|Scala (programming language)}}
[[Scala (programming language)|Scala]] 2.10+
The <code>f</code> interpolator is a compiler macro that rewrites a format string with embedded expressions as an invocation of String.format. It verifies that the format string is well-formed and well-typed.
==== The standard interpolators ====
Line 420 ⟶ 429:
<syntaxhighlight lang="output">I have 4 apples.</syntaxhighlight>
In order to actually format
<syntaxhighlight lang="tcl">
Line 431 ⟶ 440:
As of version 1.4, [[TypeScript]] supports string interpolation using backticks <code>``</code>. Here is an example:
<syntaxhighlight lang="
var apples: number = 4;
console.log(`I have ${apples} apples`);
Line 438 ⟶ 447:
<syntaxhighlight lang="output">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="
var apples: number = 4;
console.log("I have %d apples", apples);
Line 444 ⟶ 453:
The output remains the same.
=== Visual Basic .NET ===
As of Visual Basic 14, string interpolation is supported in Visual Basic.<ref>{{Cite web|last=KathleenDollard|title=Interpolated Strings - Visual Basic|url=https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/interpolated-strings|access-date=2021-06-20|website=docs.microsoft.com|language=en-us}}</ref>
<syntaxhighlight lang="
name = "Tom"
Console.WriteLine($"Hello, {name}")
</syntaxhighlight>
The output will be:
{{sxhl|Hello, Tom|output}}
== See also ==
|