Content deleted Content added
cleanup, typo(s) fixed: For example → For example, |
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 |
||
(39 intermediate revisions by 22 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 15:
== Algorithms ==
There are two main types of
# ''Replace and expand placeholders'': creating a new string from the original one, by
# ''Split and join string'': splitting the string into an array,
== Security issues ==
String interpolation, like string concatenation, may lead to security problems. If user input data is improperly escaped or filtered, the system will be exposed to [[SQL injection]], [[script injection]], [[XML
An SQL injection example:
query = "{{code|2=sql|1=SELECT x, y, z FROM Table WHERE id='$id'}} "
If ''<code>$id</code>'' is replaced with ''"<code>'; {{code|2=sql|1=DELETE FROM Table; SELECT * FROM Table WHERE id='}}</code>"'', executing this query will wipe out all the data in <code>Table</code>.
== Examples ==
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.');
print('I have ${apples+bananas}
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="output">I have 4 apples.
I have 7
=== Go ===
{{Main|Go (programming language)}}
{{as of|
=== Groovy ===
Line 120:
def quality = "superhero"
final age = 52
def sentence = "A developer is a $quality
println sentence
</syntaxhighlight>
Line 132:
var bananas = 3;
trace('I have $apples apples.');
trace('I have ${apples+bananas}
I have 4 apples.
I have 7 fruits.
</syntaxhighlight>
The output will be:▼
▲<syntaxhighlight lang="output">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>
=== 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()} ..."
</syntaxhighlight>
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;
console.log(`I have ${apples} apples`);
console.log(`I have ${apples + bananas}
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="output">
I have 4 apples
I have 7
</syntaxhighlight>
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 188 ⟶ 198:
val apples = 4
val bananas = 3
val sentence = "A developer is a $quality. I have ${apples + bananas}
println(sentence)
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="output">
A developer is a superhero. I have 7 </syntaxhighlight> === Nemerle ===
Line 266 ⟶ 278:
const Bananas := 3
Println ("I have `(Apples) apples.\n")
Println ("I have `(Apples+Bananas)
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="output">
I have 4 apples.
I have 7
</syntaxhighlight> === Perl ===
Line 292 ⟶ 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 302 ⟶ 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 319 ⟶ 331:
apples = 4
puts "I have #{apples} apples"
# Format string applications for comparison:
puts "I have %s apples" % apples
puts "I have %{a} apples" % {a: apples}
</syntaxhighlight>
Line 341 ⟶ 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 385 ⟶ 398:
apples = 4 ; bananas = 3
Output = "I have " apples " apples."
Output = "I have " (apples + bananas) "
</syntaxhighlight>
The output will be:
<syntaxhighlight lang="output">
I have 4 apples. I have 7
</syntaxhighlight> === Swift===
Line 414 ⟶ 429:
<syntaxhighlight lang="output">I have 4 apples.</syntaxhighlight>
In order to actually format
<syntaxhighlight lang="tcl">
Line 425 ⟶ 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 432 ⟶ 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 438 ⟶ 453:
The output remains the same.
=== Visual Basic .NET ===
As of Visual Basic 14,
<syntaxhighlight lang="
name = "Tom"
Console.WriteLine($"Hello, {name}")
</syntaxhighlight>
▲The output will be:
{{sxhl|Hello, Tom|output}}
== See also ==
|