format_string
Use the format_string() function to construct dynamic strings by inserting values into a predefined format.
Syntax
format_string ("<format_string>", <field_1>, <field_2>, ...<field_n>)Parameters
format_string
string
Yes
A string literal that contains zero or more format specifiers initiated by the % symbol (for example, %s, %d).
field_n
string, integer
Yes
A variable-length list of additional arguments whose values will be inserted into the format string. Each argument must match the type expected by its corresponding format specifier.
Returns
The format_string() function returns a single string containing the formatted text with argument values inserted.
Usage notes
Common format specifiers include
%sfor string values and%dfor integer values.You can use padding and width specifiers (for example,
%10d,%05d,%-10s) to control output alignment and formatting.The function requires strict type compatibility between arguments and specifiers. The function does not implicitly convert types (for example, a floating-point number passed to
%dwill fail).Use explicit type conversion functions like
to_string()orto_integer()to ensure non-matching values (like booleans or floats) are compatible with the specifiers.Note that
to_number()converts strings to floating-point numbers, which are incompatible with the%d(integer) specifier; useto_integer()instead.
Examples
Example 1: Basic string substitution (%s)
Goal: Combine literal text with string field values using the %s specifier.
XQL code:
Explanation: The event_description and dst_domain fields, which are strings, are directly inserted into the format string at the %s placeholders.
Output:
101
"Description: User login successful. Domain: ec2.amazonaws.com."
102
"Description: File access attempt. Domain: sts.amazonaws.com."
103
"Description: Network connection established. Domain: www.google.com."
Example 2: Integer substitution (%d)
Goal: Embed integer field values into a string using the %d specifier.
XQL code:
Explanation: The event_id (an integer) is converted to its string representation and inserted into the output string where %d is specified.
Output:
101
"Event #101 occurred."
102
"Event #102 occurred."
103
"Event #103 occurred."
Example 3: Combining different data types
Goal: Combine values of different original data types (boolean, float) into a single formatted string using explicit conversion.
XQL code:
Explanation: to_string(is_successful) converts the boolean to "true" or "false". to_string(duration_seconds) converts the float to a string like "1.5". format_string() then constructs the final string using these string representations for the %s specifiers.
Output:
101
true
1.5
"Successful: true, Duration: 1.5 seconds"
102
false
0.8
"Successful: false, Duration: 0.8 seconds"
103
true
10.2
"Successful: true, Duration: 10.2 seconds"
Example 4: Padding and zero-padding for integers
Goal: Utilize padding specifiers with %d and %s to control width and alignment.
XQL code:
Explanation: %05d pads event_id with leading zeros to a width of 5. %-10s left-justifies the string version within 10 characters. %s performs basic substitution.
Output:
101
ID: 00101
102
ID: 00101
103
ID: 00101
Example 5: Formatting extracted JSON scalar values with integer conversion
Goal: Extract a numeric string from JSON, convert it to an integer, and format it with %d.
XQL code:
Explanation: The query extracts the code as a string and explicitly converts it to an integer using to_integer(). This ensures compatibility with the %d specifier, which would fail if a float (from to_number) were passed.
Output:
101
{"status": "ok", "code": 200}
"API Status: 200"
102
{"status": "fail", "error": "access_denied"}
NULL
103
{"connection_id": "CONN-001", "protocol": "TCP"}
NULL
Related articles
Functions:
to_string,to_integer,concat
Last updated
Was this helpful?
