I have uploaded a test build with a new function:
string := Format(format, values*)
The format string should contain placeholders of the form {param:format}.
param: An integer corresponding to the position of a value in the parameter list, where 1 is the first parameter after format. If omitted, the number of the last placeholder + 1 is used (or 1 for the first placeholder).
format: A string controlling how the value will be formatted. The following subset of the printf format specification is supported:
- flags: - + 0 space #
width: decimal integer only
.precision: decimal integer only
type: d i o u x X e E f g G a A c C p s (not n S Z)
type defaults to 's' if omitted. 's' is a string value, not a pointer to a string.
As with Send, {{} and {}} produce literal braces. Any invalid placeholders are put into the output as is. Whitespace inside the braces is not permitted (except as a flag).
Examples:
Code: Select all
; Simple substitution
s .= format("{2}, {1}!`r`n", "World", "Hello")
; Padding with spaces
s .= format("|{:-10}|`r`n|{:10}|`r`n", "Left", "Right")
; Hexadecimal
s .= format("{1:#x} {2:X} 0x{3:x}`r`n", 3735928559, 195948557, 0)
; Floating-point
s .= format("{1:0.3f} {1:.10f}", 4*atan(1))
ListVars
WinWaitActive ahk_class AutoHotkey
ControlSetText Edit1, %s%
WinWaitClose
- The parameter index can be omitted, as in {} or {:02i}
- The type can be omitted (defaults to 's'), as in {1:5}.
- Size prefixes aren't supported, as mentioned above.
Download
---
Hexadecimal integers are always unsigned, as per normal printf behaviour but inconsistent with AutoHotkey's normal hexadecimal formatting. The '#' flag adds the 0x prefix only for non-zero values, again inconsistent with the norm. This is because the formatting of each value is done entirely by printf (to do otherwise would mean potentially much larger code and longer development time).