Skip to content

Split the string by a substring using n splits

Source code

Description

This results in a struct of n+1 fields. If it cannot make n splits, the remaining field elements will be null.

Usage

<Expr>$str$split_exact(by, n, inclusive = FALSE)

Arguments

by Substring to split by.
n Number of splits to make.
inclusive If TRUE, include the split character/string in the results.

Value

Struct where each of n+1 fields is of String type

Examples

library("polars")

df = pl$DataFrame(s = c("a_1", NA, "c", "d_4"))
df$with_columns(
  split = pl$col("s")$str$split_exact(by = "_", 1),
  split_inclusive = pl$col("s")$str$split_exact(by = "_", 1, inclusive = TRUE)
)
#> shape: (4, 3)
#> ┌──────┬─────────────┬─────────────────┐
#> │ s    ┆ split       ┆ split_inclusive │
#> │ ---  ┆ ---         ┆ ---             │
#> │ str  ┆ struct[2]   ┆ struct[2]       │
#> ╞══════╪═════════════╪═════════════════╡
#> │ a_1  ┆ {"a","1"}   ┆ {"a_","1"}      │
#> │ null ┆ {null,null} ┆ {null,null}     │
#> │ c    ┆ {"c",null}  ┆ {"c",null}      │
#> │ d_4  ┆ {"d","4"}   ┆ {"d_","4"}      │
#> └──────┴─────────────┴─────────────────┘