Skip to main content
Version: Java (Groovy)

Work with strings

Strings are sequences of characters that form words, sentences, phrases, or statements in programming languages. Deephaven queries use strings ubiquitously. Understanding their uses is critical to becoming a strong Deephaven developer.

note

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

Strings in table operations (query strings)

The first and foremost place strings get used in Deephaven queries is table operations. The strings used as inputs for table operations are typically called query strings. A query string contains a formula, which either assigns data to or filters a column by relating the column to its values through the use of one or more operators.

The following code passes the query string X = i to update.

table = emptyTable(5).update("X = i")

X = i is a query string because it relates a column (X) to its values (i) by the assignment operator =. Query strings either create, modify, or filter data from tables. The following query creates data and then filters it with query strings.

table = emptyTable(10).update("X = i").where("X % 2 == 1")

Methods like update and where can take any number of strings as input (varargs) to pass in multiple query strings. The code below uses update to add two new columns, X and Y, to an empty table.

table = emptyTable(10).update("X = i", "Y = String.valueOf(X)")

Strings in query strings

Formulas in query strings can contain strings of their own. Strings inside query strings are denoted by backticks (`). The following code uses a query string that contains another string in the where operation.

source = newTable(stringCol("X", "A", "B", "B", "C", "B", "A", "B", "B", "C"))

result = source.where("X = `C`")

String literals in query strings

Formulas in query strings can also make use of string literals. These are denoted by single quotes ('). String literals are different from normal strings in query strings because they get interpreted differently. String literals can be inferred as another data type. The following example uses both a string and a string literal in query strings to show how they differ. The string literal gets inferred as a Duration to add one second to the timestamp.

source = emptyTable(1).update("Now = now()")

result = source.update("NowPlusString = Now + `PT1s`", "NowPlusStringLiteral = Now + 'PT1s'")

Strings in tables

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

note

String columns can be created using single or double quotes. Double quotes are recommended, especially when using string literals.

result = newTable(
stringCol("X", "A", "B", "B", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "M", "N", "N", "O", "O", "P", "P", "P"),
)

String concatenation

String columns can be concatenated in queries. The following example adds two [string columns] together with +.

source = newTable(
stringCol("X", "A", "B", "B", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "M", "N", "N", "O", "O", "P", "P", "P"),
)

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

Escape characters

Strings in Deephaven support 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.

result = newTable(
stringCol("X", "Quote \" in quotes", 'Single quote \' in single quotes', "Escaped slash \\", "New\nline", "Added\ttab")
)
note

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

String filters

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".

source = newTable(stringCol("X", "Aa", "Ba", "Bb", "Ca", "Bc", "Ab", "Bd", "Be", "Cb"))

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