A string
can be created with concatenation. This sometimes requires many statements. It can be complex. With a format string
we build more complex strings.
In Ruby we apply the string
format syntax (the "%" operator) to ease this creation of formatted string
data. After the "%" we specify arguments.
We use the percentage sign ("%") to specify a format code within a string
. And we also apply the "%" sign after the string
, to indicate the arguments.
string
code. We specify the two values in an array.# Use digit format. format = "Number is %d" % 12 puts format # Use two formatting codes. format = "Number is %d, type is %s" % [13, "cat"] puts formatNumber is 12 Number is 13, type is cat
A format string
can pad strings. We specify the total width of the desired string
. And an amount of padding (space characters) is added on the left or right side to fit the size.
string
is aligned to the right. To have a 10 char
string
, use the code "%10s."# Align string to the right (pad the left). right = "[%10s]" % "carrot" puts right # Align to the left. left = "[%-10s]" % "carrot" puts left[ carrot] [carrot ]
Often when representing floating-point numbers as strings, we want to control the number of digits past the decimal.
# Use two digits after the decimal place for floating-point. result = "Result is %.2f" % 12.3456 p result # Use three digits. result = "Result is %.3f" % 12.3456 p result"Result is 12.35" "Result is 12.346"
String
interpolationThis is another kind of format syntax. When we use the "#" and curly-brackets in a string
literal, we can capture variables and format them as strings.
name = "Plato" id = 65 # Use string interpolation to format variables. # ... The names specified in brackets must match exactly. result = "Name is #{name}, ID is #{id}" p result"Name is Plato, ID is 65"
We can specify a mapping from string
format identifiers to actual program variables. Here we specify "b" means the breed variable and "z" means size.
string
format pattern.string
does not need changing, just the mapping.breed = "Spaniel" size = 65 # Use names in format syntax. # ... We use "b" as the name for breed and "z" for size. result = "Breed %{b} size %{z}" % {b: breed, z: size} p result"Breed Spaniel size 65"
Kernel::sprintf
The string
format syntax, which uses the "%" symbol, calls into the Kernel::sprintf
method. We can use sprintf or format()
explicitly, but there is no benefit to this.
# String format syntax. result = "There are %d units." % 10 puts result # Call sprintf for equivalent functionality. result = Kernel::sprintf("There are %d units.", 10) puts result # Call format. result = Kernel::format("There are %d units.", 10) puts resultThere are 10 units. There are 10 units. There are 10 units.
Is string
interpolation fast? I compared this syntax to a concatenation of 4 values. I found interpolation was faster.
string
interpolation syntax to combine 4 values together.string
concatenation with the plus operator to merge 4 values into 1 string
. This version was slower.String
interpolation is a clear performance winner in these simple tests. I recommend it for most Ruby programs.string
format syntax, with "%s" and "%d" and found it to be even slower than concat.n1 = Time.now.usec # Version 1: use string interpolation syntax. 90000.times do cat = "Felix" size = 100 result = "Name: #{cat}, size: #{size}" end n2 = Time.now.usec # Version 2: use string concatenation. 90000.times do cat = "Felix" size = 100 result = "Name: " + cat + ", size: " + String(size) end n3 = Time.now.usec # Total milliseconds. puts ((n2 - n1) / 1000) puts ((n3 - n2) / 1000)44 ms: String interpolation syntax 71 ms: String concat (plus)
Most requirements in programming are not glamorous. They involve no inventive algorithms. Instead, we often format strings.
In Ruby the string
format syntax helps make this mundane stuff easier. A method (sprintf) can be used instead of the "%" symbol.