Skip to content

Split the string by a substring, restricted to returning at most n items

Source code

Description

If the number of possible splits is less than n-1, the remaining field elements will be null. If the number of possible splits is n-1 or greater, the last (nth) substring will contain the remainder of the string.

Usage

<Expr>$str$splitn(by, n)

Arguments

by Substring to split by.
n Number of splits to make.

Value

Struct where each of n fields is of String type

Examples

library(polars)

df = pl$DataFrame(s = c("a_1", NA, "c", "d_4_e"))
df$with_columns(
  s1 = pl$col("s")$str$splitn(by = "_", 1),
  s2 = pl$col("s")$str$splitn(by = "_", 2),
  s3 = pl$col("s")$str$splitn(by = "_", 3)
)
#> shape: (4, 4)
#> ┌───────┬───────────┬─────────────┬──────────────────┐
#> │ s     ┆ s1        ┆ s2          ┆ s3               │
#> │ ---   ┆ ---       ┆ ---         ┆ ---              │
#> │ str   ┆ struct[1] ┆ struct[2]   ┆ struct[3]        │
#> ╞═══════╪═══════════╪═════════════╪══════════════════╡
#> │ a_1   ┆ {"a_1"}   ┆ {"a","1"}   ┆ {"a","1",null}   │
#> │ null  ┆ {null}    ┆ {null,null} ┆ {null,null,null} │
#> │ c     ┆ {"c"}     ┆ {"c",null}  ┆ {"c",null,null}  │
#> │ d_4_e ┆ {"d_4_e"} ┆ {"d","4_e"} ┆ {"d","4","e"}    │
#> └───────┴───────────┴─────────────┴──────────────────┘