String Interpolation
or variable binding
C++20
via std::format
Crystal
Work alike ruby with either #{}
a = 1
b = 2
"sum: #{a} + #{b} = #{a + b}" # => "sum: 1 + 2 = 3"
or %{} (native similar to Mustache).
"sum: %{one} + %{two} = %{three}" % {one: 1, two: 2, three: 1 + 2} # => "sum: 1 + 2 = 3"
D
Lua
Lua doesn’t have built-in string interpolation
Using string.format (most common way)
local name = "Alice"
local age = 25
local msg = string.format("My name is %s and I am %d years old.", name, age)
print(msg)
Using concatenation (..)
name = "James"
puts "Hello, #{name}" // Hello, James
Custom interpolation function
function interpolate(str, vars)
return (str:gsub("($%b{})", function(w)
return vars[w:sub(3, -2)] or w
end))
end
local name, age = "Alice", 25
local msg = interpolate("My name is ${name} and I am ${age} years old.", {name=name, age=age})
print(msg)
Ruby
name = "James"
puts "Hello, #{name}" // Hello, James
- On data read from a file - (native similar to Mustache).
vars = { name: "James"}
mystring = "Hello, %{name}" // content of file, using %{} tags
mystring % vars // => Hello, James, == string.format
Scala
val name = "James"
println(s"Hello, $name") // Hello, James
Javascript/Typescript
let value = 100;
console.log(`The size is ${ value }`);
m4
maven
- parameter expansion -> see maven filtering
templates
Written on August 27, 2018, Last update on August 30, 2023
crystal
ruby
lua
scala
dlang
string
interpolation