split_part()#

relationalai.std.strings
#split_part(string: str | Producer, separator: str | Producer, index: int | Producer) -> Expression

Gets a substring of a string that is separated by a separator at a given index. Must be called in a rule or query context.

Parameters#

NameTypeDescription
stringstr or ProducerThe string to split.
separatorstr or ProducerThe separator to split the string by.
indexint or ProducerThe zero-based index of the substring to return.

Returns#

An Expression object that producer string values.

Example#

#import relationalai as rai
from relationalai.std.strings import split_part

# Create a model named "people" with a Person type.
model = rai.Model("people")
Person = model.Type("Person")

# Add some people to the model.
with model.rule():
    alice = Person.add(name="Alice Smith")
    bob = Person.add(name="Bob Jones")

# Create 'first' and 'last' properties by splitting the 'name' property.
with model.rule():
    person = Person()
    person.set(
        first=split_part(person.name, " ", 0),
        last=split_part(person.name, " ", 1)
    )

# Query the 'first' and 'last' properties.
with model.query() as select:
    person = Person()
    response = select(person.first, person.last)

print(response.results)
# Output:
#    first   last
# 0  Alice  Smith
# 1    Bob  Jones

See Also#