Skip to content

Get the number of bytes in strings

Source code

Description

Get length of the strings as UInt32 (as number of bytes). Use $str$len_chars() to get the number of characters.

Usage

<Expr>$str$len_bytes()

Details

If you know that you are working with ASCII text, lengths will be equivalent, and faster (returns length in terms of the number of bytes).

Value

Expr of u32

Examples

library(polars)

pl$DataFrame(
  s = c("Café", NA, "345", "æøå")
)$select(
  pl$col("s"),
  pl$col("s")$str$len_bytes()$alias("lengths"),
  pl$col("s")$str$len_chars()$alias("n_chars")
)
#> shape: (4, 3)
#> ┌──────┬─────────┬─────────┐
#> │ s    ┆ lengths ┆ n_chars │
#> │ ---  ┆ ---     ┆ ---     │
#> │ str  ┆ u32     ┆ u32     │
#> ╞══════╪═════════╪═════════╡
#> │ Café ┆ 5       ┆ 4       │
#> │ null ┆ null    ┆ null    │
#> │ 345  ┆ 3       ┆ 3       │
#> │ æøå  ┆ 6       ┆ 3       │
#> └──────┴─────────┴─────────┘