A template is based text that contains special notation to be replaced with meaningful content. This can be created as separate files or embedded into code. Templates can be used for simple things like print a message or generate complex content.

String format

We use the hashtag character “#” to create a placeholder format into a string. We use string format operator “<-” to replace placeholders with numeric or string values. This operator can use a variable to resolve a template.  It can be used with single variables or with tuples.

print ('the number has value #n and is #s' <- (10,'even'))

⇒ the number has value 10 and is even

String placeholders

  • #s is used for strings
  • #n is used for numbers
  • #d is used for date
  • #t is used for time
  • #h is used for control

Using placeholder (#) into a string

When placeholders are presented into the string template the members of the collection are used to fill-in the places with values: In the following example we have 5 placeholders. Last placeholder is used to insert to CR.

output ('#s is #n and #s is #n.' <- ('one',1,'two',2));

=> one is 1 two is 2.

If the format string # is followed by round parentheses #(…). the string is called a template. We can use inside parentheses several other symbols to be more specific about conversion. This can be used to align strings and numbers.

#(x, ap:m.d).

Details:

  • x is one of {s,n,f,t}
  • a is alignment one of {<,>,^},
  • p is the padding character: {‘_’,0}
  • m is the length
  • d is number of decimals

Alignment symbol “a” can be one of:

> is used to align to the right
< is used to align to the left
^ is used to align to center

Format examples:

'#(s, >_:10)'   ; right align string fill with spaces to 10 characters
'#(n, >0:10.2)' ; right align fill with 0 up to 10 digits and use 2 decimals

You need to login to view the rest of the content. Please . Not a Member? Join Us