Skip to main content
Version: Python

How to work with strings

Strings are sequences of characters that form words, sentences, phrases, or statements in programming languages. The importance of strings in the Deephaven Query Language cannot be understated. Strings are used universally within Deephaven for modifying table data. Queries are written using strings, and most table operations require the use of query strings. Understanding their proper use is critical to becoming a strong Deephaven developer.

note

This guide assumes you are familiar with the use of strings in Python. If not, please refer to the Python documentation for more information.

Strings in tables

In the following example, a new table is created with two string columns.

note

String values can be created using single or double quotes.

from deephaven import new_table
from deephaven.column import string_col

result = new_table(
[
string_col("X", ["A", "B", "B", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "M", "N", "N", "O", "O", "P", "P", "P"]),
]
)

String concatenation

Deephaven supports string concatenation in queries. In the following example, the string columns are added together with the + operator to make a new column.

from deephaven import new_table
from deephaven.column import string_col

source = new_table(
[
string_col("X", ["A", "B", "B", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "M", "N", "N", "O", "O", "P", "P", "P"]),
]
)

result = source.update(formulas=["Add = X + Y"])

Strings in query strings

Deephaven query strings can be used to filter data and to create new columns. To express string literal values within a query string, backticks (`) are used.

The following example shows how to filter a string column for strings that match "C".

from deephaven import new_table
from deephaven.column import string_col

source = new_table([string_col("X", ["A", "B", "B", "C", "B", "A", "B", "B", "C"])])

result = source.where(filters=["X = `C`"])

Escape character

Deephaven supports the escape character \. The escape character invokes alternative interpretations of the characters that follow it. For example, \n is a new line and not the character n. Similarly, \t is a tab and not the character t.

The query below shows how Deephaven responds to these characters.

note

For more information on escaping characters in strings, see the Python documentation.

from deephaven import new_table
from deephaven.column import string_col

result = new_table(
[
string_col(
"X",
[
'Quote " in quotes',
"Single quote ' in single quotes",
"Escaped slash \\",
"New\nline",
"Added\ttab",
],
)
]
)

Advanced string operations in queries

Deephaven supports using java.lang.String methods on strings in queries.

The following example shows how to filter a string column for values that start with "C".

from deephaven import new_table
from deephaven.column import string_col

source = new_table(
[string_col("X", ["Aa", "Ba", "Bb", "Ca", "Bc", "Ab", "Bd", "Be", "Cb"])]
)

result = source.where(filters=["X.startsWith(`C`)"])