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.
This guide assumes you are familiar with the use of strings in Python and Groovy. If not, please refer to the Python documentation or the Groovy documentation for more information.
Strings in tables
In the following example, a new table is created with two string columns.
String values can be created using single or double quotes.
result = newTable(
stringCol("Strings", "Deephaven", "3.14", "Community")
)
- result
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.
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")
- source
- result
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"
.
source = newTable(
stringCol("X", "A", "B", "B", "C", "B", "A", "B", "B", "C")
)
result = source.where("X = `C`")
- source
- result
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.
For more information on escaping characters in strings, see the Python documentation or the Groovy documentation.
result = newTable(
stringCol("X", "Quote \" in quotes", 'Single quote \' in single quotes', "Escaped slash \\", "New\nline", "Added\ttab")
)
- result
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"
.
source = newTable(
stringCol("X", "Aa", "Ba", "Bb", "Ca", "Bc", "Ab", "Bd", "Be", "Cb")
)
result = source.where("X.startsWith(`C`)")
- source
- result