Skip to content

Split the string by a substring

Source code

Description

Split the string by a substring

Usage

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

Arguments

by Substring to split by. Can be an Expr.
inclusive If TRUE, include the split character/string in the results.

Value

List of String type

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(pl$col("s")$str$split(by = pl$col("by"))$alias("split"))
#> shape: (3, 1)
#> ┌───────────────────────┐
#> │ split                 │
#> │ ---                   │
#> │ list[str]             │
#> ╞═══════════════════════╡
#> │ ["foo^bar"]           │
#> │ ["foo", "bar"]        │
#> │ ["foo", "bar", "baz"] │
#> └───────────────────────┘