Strings
A String is a sequence of characters, such as letters, numbers, or symbols, that is treated as a single data type. Strings frequently contain words or text. Deephaven queries use strings ubiquitously. The Deephaven engine uses java.lang.String
but is designed to interoperate with Python strings as well.
This guide focuses on the use of both Python and Java strings interchangeably in queries.
Python strings in queries
Python strings are analogous to Java strings, as they share many similarities. Python string variables can be passed into table operations, and the engine knows how to handle them without any additional input. Note the column types. The Python strings are automatically converted to Java java.lang.String
strings.
from deephaven import empty_table
my_str = "Deephaven!"
source = empty_table(1).update("MyStringColumn = `Hello, ` + my_str")
source_meta = source.meta_table
Python functions that return strings are no different:
from deephaven import empty_table
def string_function() -> str:
return "Hello, Deephaven!"
source = empty_table(1).update("StringColumn = string_function()")
source_meta = source.meta_table
Java strings in queries
String columns in Deephaven tables are stored as java.lang.String
objects. This provides three powerful ways to manipulate text in query strings:
- Native Java string methods (like
length
,substring
, etc.) - Deephaven's built-in string functions
- String-specific operators (such as
+
for concatenation)
String concatenation
Like Python strings, java.lang.String
objects can be concatenated with the +
operator:
from deephaven import empty_table
source = empty_table(1).update(
["StrCol1 = `Hello, `", "StrCol2 = `Deephaven!`", "StrCol3 = StrCol1 + StrCol2"]
)
source_meta = source.meta_table
String methods
java.lang.String
methods can be called from query strings and can yield very powerful queries:
from deephaven import empty_table
source = empty_table(1).update(
[
"StrCol1 = `Learning `",
"StrCol2 = `about `",
"StrCol3 = ` strings.`",
"StrCol4 = StrCol1 + StrCol2 + StrCol3",
]
)
result = source.update(
[
"IndexOfSubstring = StrCol4.indexOf(`ings.`)",
"StringIsEmpty = StrCol2.isEmpty()",
"Substring = StrCol4.substring(4, 14)",
"UpperCase = StrCol1.toUpperCase()",
"LowerCase = StrCol3.toLowerCase()",
]
)
String filtering
A common use of string methods is for string filtering:
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`)"])
Built-in functions
Many built-in functions work with strings. For instance, you can parse the numeric values in strings:
from deephaven import empty_table
source = empty_table(1).update(
[
"StringInt = `4`",
"StringFloat = `4.5`",
"StringDouble = `3.14159`",
"StringBool = `true`",
]
)
result = source.update(
[
"Int = parseInt(StringInt)",
"Float = parseFloat(StringFloat)",
"Double = parseDouble(StringDouble)",
"Bool = parseBoolean(StringBool)",
]
)
result_meta = result.meta_table