GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — String interpolation - Wikipedia 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 placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing[1] or, in formal terms, a form of quasi-quotation (or logic substitution interpretation). The placeholder may be a variable name, or in some languages an arbitrary expression, in either case evaluated in the current context. String interpolation is an alternative to building a string via concatenation, which requires repeat quoting and unquoting;[2] or substituting into a printf format string, where the variable is far from where it is used. Consider this example in Ruby: apples = 4 puts "I have #{apples} apples." # string interpolation puts "I have " + String(apples) + " apples." # string concatenation puts "I have %d apples." % apples # format string Two types of literal expression are usually offered: one with interpolation enabled, the other without. Non-interpolated strings may also escape sequences, in which case they are termed a raw string, though in other cases this is separate, yielding three classes of raw string, non-interpolated (but escaped) string, interpolated (and escaped) string. For example, in Unix shells, single-quoted strings are raw, while double-quoted strings are interpolated. Placeholders are usually represented by a bare or a named sigil (typically Language support for string interpolation varies widely. Some languages do not offer string interpolation, instead using concatenation, simple formatting functions, or template libraries. String interpolation is common in many programming languages which make heavy use of string representations of data, such as Apache Groovy, Julia, Kotlin, Perl, PHP, Python, Ruby, Scala, Swift, Tcl and most Unix shells. AlgorithmsThere are two main types of variable-expanding algorithms for variable interpolation:[3]
Security issuesSee also: Uncontrolled format string
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 external entity (XXE) injection, and cross-site scripting (XSS) attacks.[4] An SQL injection example: query = "
If ExamplesABAPMain article: ABAP
DATA(apples) = 4. WRITE |I have { apples } apples|. The output will be: I have 4 apples
BashMain article: Bash (Unix shell)
apples=4 echo "I have $apples apples" # or echo "I have ${apples} apples" The output will be: I have 4 apples
BooMain article: Boo (programming language)
apples = 4 print("I have $(apples) apples") # or print("I have {0} apples" % apples) The output will be: I have 4 apples
CMain article: C (programming language)
C does not have interpolated strings, but they can be approximated using C++Main article: C++
While interpolated strings do not exist in C++, some proposals for the feature exist[5][6]; currently, they can be approximated using C#Main article: C Sharp (programming language)
using System; int apples = 4; int bananas = 3; Console.WriteLine($"I have {apples} apples"); Console.WriteLine($"I have {apples + bananas} fruits"); The output will be: I have 4 apples I have 7 fruits ColdFusion Markup LanguageMain article: ColdFusion Markup Language
ColdFusion Markup Language (CFML) script syntax: apples = 4; writeOutput("I have #apples# apples"); Tag syntax: <cfset apples = 4> <cfoutput>I have #apples# apples</cfoutput> The output will be: I have 4 apples CoffeeScriptMain article: CoffeeScript
apples = 4 console.log "I have #{apples} apples" The output will be: I have 4 apples
DartMain article: Dart (programming language)
int apples = 4, bananas = 3; print('I have $apples apples.'); print('I have ${apples+bananas} fruits.'); The output will be: I have 4 apples. I have 7 fruits. GoMain article: Go (programming language)
While there have been some proposals for string interpolation (which have been rejected),[8][9][10] As of 2025 Go does not have interpolated strings. However, they can be approximated using GroovyMain article: Groovy (programming language)
In groovy, interpolated strings are known as GStrings:[11] def quality = "superhero" final age = 52 def sentence = "A developer is a $quality if he is ${age <= 42 ? 'young' : 'seasoned'}" println sentence The output will be: A developer is a superhero if he is seasoned
HaxeMain article: Haxe
var apples = 4; var bananas = 3; trace('I have $apples apples.'); trace('I have ${apples+bananas} fruits.'); The output will be:[12] I have 4 apples. I have 7 fruits. JavaMain article: Java (programming language)
Java had interpolated strings as a preview feature in Java 21 and Java 22 (see JavaScript/TypeScriptMain articles: JavaScript and TypeScript
JavaScript and TypeScript, as of the ECMAScript 2015 (ES6) standard, support string interpolation using backticks const apples: number = 4; const bananas: number = 3; console.log(`I have ${apples} apples`); console.log(`I have ${apples + bananas} fruits`); The output will be: I have 4 apples I have 7 fruits JuliaMain article: Julia (programming language)
apples = 4 bananas = 3 print("I have $apples apples and $bananas bananas, making $(apples + bananas) pieces of fruit in total.") The output will be: I have 4 apples and 3 bananas, making 7 pieces of fruit in total.
KotlinMain article: Kotlin (programming language)
fun main() { val quality: String = "superhero" val apples: Int = 4 val bananas: Int = 3 val sentence: String = "A developer is a $quality. I have ${apples + bananas} fruits" println(sentence) } The output will be: A developer is a superhero. I have 7 fruits
NemerleMain article: Nemerle
def apples = 4; def bananas = 3; Console.WriteLine($"I have $apples apples."); Console.WriteLine($"I have $(apples + bananas) fruit."); It also supports advanced formatting features, such as: def fruit = ["apple", "banana"]; Console.WriteLine($<#I have ..$(fruit; "\n"; f => f + "s")#>); The output will be: apples bananas NimMain article: Nim (programming language)
Nim provides formatted string literals via the strformat module. The import strformat var apples = 4 echo fmt"I have {apples} apples" echo fmt"I have {apples + bananas} fruits" The output will be: I have 4 apples I have 7 fruits NixMain article: Nix package manager
let numberOfApples = "4"; in "I have ${numberOfApples} apples" The output will be: I have 4 apples
ParaSailMain article: ParaSail (programming language)
const Apples := 4 const Bananas := 3 Println ("I have `(Apples) apples.\n") Println ("I have `(Apples+Bananas) fruits.\n") The output will be: I have 4 apples. I have 7 fruits. PerlMain article: Perl
my $apples = 4; my $bananas = 3; print "I have $apples apples.\n"; print "I have @{[$apples+$bananas]} fruit.\n"; # Uses the Perl array (@) interpolation. The output will be: I have 4 apples. I have 7 fruit. PHPMain article: PHP
<?php $apples = 5; $bananas = 3; echo "There are $apples apples and $bananas bananas.\n"; echo "I have {$apples} apples and {$bananas} bananas."; The output will be: There are 5 apples and 3 bananas. I have 5 apples and 3 bananas. PythonMain article: Python (programming language)
Python supports string interpolation as of version 3.6, referred to as
"formatted string literals" or "f-strings".[15][16][17] Such a literal begins with an apples: int = 4 bananas: int = 3 print(f"I have {apples} apples and {bananas} bananas") The output will be: I have 4 apples and 3 bananas
Ruby/CrystalMain articles: Ruby (programming language) and Crystal (programming language)
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} The output will be: I have 4 apples
RustMain article: Rust (programming language)
Rust does not have general string interpolation, but provides similar functionality via macros, referred to as "Captured identifiers in format strings", introduced in version 1.58.0, released 2022-01-13.[18] Rust provides formatting via the fn main() { let (apples, bananas): (i32, i32) = (4, 3); // println! captures the identifiers when formatting: the string itself isn't interpolated by Rust. println!("There are {apples} apples and {bananas} bananas."); // alternatively, with format!(): let sentence: String = format!("There are {0} apples and {1} bananas.", apples, bananas); println!(sentence); } The output will be: There are 4 apples and 3 bananas.
ScalaMain article: Scala (programming language)
Scala provides a general facility to allow arbitrary processing of a string literal, and supports string interpolation using the included The Scala's string interpolation allows embedding variable references directly in processed string literals. Here is an example: val apples = 4 val bananas = 3 println(s"I have $apples apples") println(s"I have ${apples + bananas} fruits") println(f"I have $apples%d apples") The output will be: I have 4 apples I have 7 fruits I have 4 apples Sciter (tiscript)In Sciter any function with name starting from $ is considered as interpolating function and so interpolation is customizable and context sensitive: var apples = 4 var bananas = 3 var domElement = ...; domElement.$content(<p>I have {apples} apples</p>); domElement.$append(<p>I have {apples + bananas} fruits</p>); Where domElement.$content(<p>I have {apples} apples</p>); gets compiled to this: domElement.html = "<p>I have " + apples.toHtmlString() + " apples</p>"; SwiftMain article: Swift (programming language)
In Swift, a new String value can be created from a mix of constants, variables, literals, and expressions by including their values inside a string literal.[19] Each item inserted into the string literal is wrapped in a pair of parentheses, prefixed by a backslash. let apples = 4 print("I have \(apples) apples") The output will be: I have 4 apples
TclMain article: Tcl (programming language)
The Tool Command Language has always supported string interpolation in all quote-delimited strings. set apples 4 puts "I have $apples apples." The output will be: I have 4 apples.
In order to actually format – and not simply replace – the values, there is a formatting function. set apples 4 puts [format "I have %d apples." $apples] Visual Basic .NETAs of Visual Basic 14, string interpolation is supported in Visual Basic.[20] name = "Tom" Console.WriteLine($"Hello, {name}") The output will be: Hello, Tom
See alsoNotes
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |