relationalai.std.strings.split()
split(string: str|Producer, separator: str|Producer) -> tuple[Expression]
Splits a string into substrings based on a separator.
Returns a tuple of two Expression
objects, where the first produces the index of the substring and the second produces the substring itself.
If string
or separator
is a Producer
, then split()
acts as a filter and removes non-string values from the producer.
Must be called in a rule or query context.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
string | Producer or Python str object | The string to split. |
separator | Producer or Python str object | The separator to split the string by. |
Returns
Section titled “Returns”A tuple of two Expression
objects.
Example
Section titled “Example”Use split()
to split a string into substrings based on a separator:
import relationalai as raifrom relationalai.std import strings
# =====# SETUP# =====
model = rai.Model("MyModel")Person = model.Type("Person")
with model.rule(): Person.add(id=1).set(full_name="Alice Smith") Person.add(id=2).set(full_name="Bob Jones") Person.add(id=3).set(full_name=-1) # Non-string full_name
# =======# EXAMPLE# =======
# Set first_name and last_name properties by splitting the name property.with model.rule(): person = Person() index, substring = strings.split(person.full_name, " ") # Set first_name to the substring with index 0. with index == 0: person.set(first_name=substring) # Set last_name to the substring with index 1. with index == 1: person.set(last_name=substring)
# Since split() filters out non-string values, the first_name and last_name# properties are not set for the person with id=3.with model.query() as select: person = Person() response = select(person.id, person.first_name, person.last_name)
print(response.results)# id first_name last_name# 0 1 Alice Smith# 1 2 Bob Jones# 2 3 NaN NaN