Strings and bytes#
Strings are sequences of Unicode code points. Bytes represent raw binary data.
String literals#
Use double quotes or single quotes for standard strings.
Use triple-quotes to create multi-line strings.
Raw strings use the r prefix and preserve content literally.
Escape sequences#
Standard strings interpret escape sequences. Raw strings do not.
| Sequence | Description |
|---|---|
\\ |
Backslash |
\' |
Single quote |
\" |
Double quote |
\` |
Backtick |
\a |
Bell |
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Tab |
\v |
Vertical tab |
\xHH |
Hex byte (2 digits) |
\000 |
Octal byte (3 digits) |
\uHHHH |
Unicode code point (BMP) |
\UHHHHHHHH |
Unicode code point (any plane) |
Byte literals#
Use the b prefix with a string literal.
Bytes support the same escape sequences as strings, except for Unicode escapes (\u and \U).
Size#
size() returns the length.
For strings, it counts code points.
For bytes, it counts bytes.
A single emoji is one code point but multiple bytes.
Concatenation#
+ joins strings or bytes together.
Search#
contains() checks if a string includes a substring.
Matching is case-sensitive.
startsWith() and endsWith() check string boundaries.
Regular expressions#
matches() tests if a regular expression matches anywhere in the string.
CEL uses RE2 syntax for regular expressions.
Use anchors to match the entire string.
Conversion#
Convert between strings and bytes.
Converting bytes to string requires valid UTF-8.
Convert other types to strings.
Convert strings to other types.
Format extension#
The CEL specification defines a string formatting extension that provides format() for printf-style string formatting.
Extensions must be enabled in the CEL runtime.
format() takes a single list argument containing the values to substitute.
The function is not variadic.
| Verb | Description | Precision |
|---|---|---|
%s |
String representation | No |
%d |
Decimal integer | No |
%f |
Floating-point | Yes (default 6) |
%e |
Scientific notation | Yes (default 6) |
%x |
Hexadecimal (lowercase) | No |
%X |
Hexadecimal (uppercase) | No |
%o |
Octal | No |
%b |
Binary | No |
Use .N to specify precision.
Format errors#
Passing a non-list argument produces a compile error.
Providing fewer arguments than format verbs produces an error.
Using the wrong type for a format verb produces an error.
"%d".format(["hello"])
// error: decimal clause can only be used on ints, uints, and doubles, was given string
See also#
- Comparison operators - Equality and ordering