The Format string gives you control over a string's appearance. For example, you can specify the length of a string, the fill character to use, and its alignment.
A format string consists of a sequence of characters used as placeholders and, optionally, control characters. All other characters in the format string are treated as literals and will be copied to the output string. When the input string contains more characters than the number of placeholders defined in the format string, the remaining characters will be copied to the output string without formatting.
All strings are treated as text strings. The alignment is right to left by default, but can be changed (to left to right) by preceding the format string by a ^.
Placeholder | Description | Input string | Format | Output string |
---|---|---|---|---|
# | Display a character or a zero. If the string has a character in the position where the # appears in the format string, display it; otherwise, display a zero in that position. | "12345" "123456" |
######### ##-##-## |
000012345 "12-34-56" |
_ | Display a character or a space. If the string has a character in the position where the _ appears in the format string, display it; otherwise, display a space in that position. | "AB123" | _{8} | " AB123" |
& | Display a character or nothing. If the string has a character in the position where the & appears, display it; otherwise, display nothing. | " AB123" | &{8} | "AB123" |
Control Character | Description | Input string | Format | Output string |
---|---|---|---|---|
{ n } | Repeats the previous element n times. | "123" | #{5} | "00123" |
^ | Force left to right fill of placeholders. The default is to fill from right to left. | "1234" "ABCD" |
^####.## ^_{8}" |
"1234.00" "ABCD " |