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 simple emoji is one code point but multiple bytes.
Concatenation#
+ joins strings or bytes together.
Substring 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.
Strings extension#
The CEL strings extension adds string manipulation functions. Extensions must be enabled in the CEL runtime, which may not be supported.
Case conversion#
lowerAscii() converts ASCII characters to lowercase.
Non-ASCII characters are unchanged.
upperAscii() converts ASCII characters to uppercase.
Non-ASCII characters are unchanged.
Position search#
indexOf() returns the position of the first occurrence of a substring.
Returns -1 if not found.
An optional second argument specifies the position to start searching from.
lastIndexOf() returns the position of the last occurrence of a substring.
Returns -1 if not found.
An optional second argument specifies the last position to consider as the start of a match.
Character access#
charAt() returns the character at the given position in the string.
The position must be between zero and the string length, inclusive.
When the position equals the string length, an empty string is returned.
Substring#
substring() returns a portion of the string from a start index.
An optional second argument specifies the end index (exclusive).
Replace#
replace() replaces occurrences of a search string with a replacement.
An optional third argument limits the number of replacements.
Split and join#
split() divides a string into a list of substrings by a separator.
An optional second argument limits the number of splits.
join() is a method on string lists that concatenates elements into a single string.
An optional argument specifies a separator.
Trim#
trim() removes leading and trailing whitespace using the Unicode definition of whitespace.
Reverse#
reverse() returns a new string with the code points in reverse order.
Quote#
strings.quote() is a global function (not a method) that escapes a string so it is safe to print.
Invalid UTF-8 characters are replaced with \uFFFD.
Format#
format() provides printf-style string formatting.
It 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