Skip to content

Split the string by a substring

Description

Split the string by a substring

Usage

<Expr>$str$split(by, ..., inclusive = FALSE)

Arguments

by Substring to split by. Can be an Expr.
These dots are for future extensions and must be empty.
inclusive If TRUE, include the split character/string in the results.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(s = c("foo bar", "foo-bar", "foo bar baz"))
df$select(pl$col("s")$str$split(by = " "))
#> shape: (3, 1)
#> ┌───────────────────────┐
#> │ s                     │
#> │ ---                   │
#> │ list[str]             │
#> ╞═══════════════════════╡
#> │ ["foo", "bar"]        │
#> │ ["foo-bar"]           │
#> │ ["foo", "bar", "baz"] │
#> └───────────────────────┘
df <- pl$DataFrame(
  s = c("foo^bar", "foo_bar", "foo*bar*baz"),
  by = c("_", "_", "*")
)
df
#> shape: (3, 2)
#> ┌─────────────┬─────┐
#> │ s           ┆ by  │
#> │ ---         ┆ --- │
#> │ str         ┆ str │
#> ╞═════════════╪═════╡
#> │ foo^bar     ┆ _   │
#> │ foo_bar     ┆ _   │
#> │ foo*bar*baz ┆ *   │
#> └─────────────┴─────┘
df$select(split = pl$col("s")$str$split(by = pl$col("by")))
#> shape: (3, 1)
#> ┌───────────────────────┐
#> │ split                 │
#> │ ---                   │
#> │ list[str]             │
#> ╞═══════════════════════╡
#> │ ["foo^bar"]           │
#> │ ["foo", "bar"]        │
#> │ ["foo", "bar", "baz"] │
#> └───────────────────────┘