typestar

stringr in R

Consistent, pipe-friendly string functions.

library(stringr)

names <- c("Ada Lovelace", "Grace Hopper")

lengths <- str_length(names)
upper <- str_to_upper(names)
titled <- str_to_title("ada lovelace")

has_grace <- str_detect(names, "Grace")
surnames <- str_extract(names, "\\w+$")
swapped <- str_replace(names, "Ada", "A.")
parts <- str_split(names, " ")
padded <- str_pad(names, 20, side = "right")
trimmed <- str_trim("  spaced  ")

How it works

  1. Every function starts str_ and takes the string first.
  2. str_detect, str_extract, str_replace use patterns.
  3. str_pad and str_trim handle whitespace.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
392
Tokens
92
Three-star pace
100 tpm

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

Type this snippet

Step 1 of 3 in stringr, step 5 of 10 in Functional & text tools.

← Previous Next →