typestar

stringr in R

Consistent argument order: the string first, the pattern second.

library(stringr)

files <- c("r/dp_select.R", "python/pd_merge.py", "go/go_test_basic.go")

print(str_detect(files, "^r/"))
print(str_replace(files, "^[a-z]+/", ""))
print(str_extract(files, "\\.[a-z]+$"))
print(str_split(files, "/", simplify = TRUE))
print(str_pad(c("r", "python"), width = 8, side = "right", pad = "."))
print(str_to_title("language basics"))

lang <- "r"
steps <- 138
print(str_glue("{lang} has {steps} steps"))

How it works

  1. Every function is str_ plus a verb.
  2. str_detect, str_replace and str_extract cover most work.
  3. str_glue interpolates, like an f-string.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
431
Tokens
115
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 69 seconds.

Type this snippet

Step 2 of 3 in stringr, step 6 of 10 in Functional & text tools.

← Previous Next →