stringr::str_starts() and stringr::str_ends()
   get_help() docs


Description

The str_starts() and str_ends() functions are part of the {stringr} package, which is part of the {tidyverse}.

We use these functions to ask if it is TRUE or FALSE that a certain string either starts with (str_starts()) or ends with (str_end()) a given pattern. A common usage of this function is with dplyr::filter() to subset rows from tibbles (data frames) that start and/or end with a certain set of characters.

All {stringr} functions (and R itself) are case sensitive, which means uppercase and lowercase letters are viewed as different characters. In other words, searching for “A” will not match “a”, and searching for “a” will not match “A.”

To use this function, you need to either first load the {stringr} library, or always use the function with stringr:: notation.

# Load the library
library(stringr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation, for example with str_starts() 
stringr::str_starts()

Conceptual Usage

stringr::str_starts("string to search in", 
                    "substring you are asking if the first argument starts with")

stringr::str_starts(c("array", "of", "strings", "to", "search", "in"), 
                   "substring you are asking if the first argument starts with")

stringr::str_ends("string to search in", 
                    "substring you are asking if the first argument ends with")

stringr::str_ends(c("array", "of", "strings", "to", "search", "in"), 
                   "substring you are asking if the first argument ends with")

Examples

The examples use the variables shown below, as well as the carnivores dataset. Learn more about the carnivores with get_help("carnivores").

# Show all variables and datasets used in examples:

# A single string
single_sentence
## [1] "No doubt about the way the wind blows."

# An array of strings
fruits
## [1] "kiwi fruit" "pomelo"     "goji berry" "persimmon"  "mulberry"   "raspberry"

# A tibble
carnivores
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Cheetah           Acinonyx      11.9 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Northern fur seal Callorhinus   15.3 NA     
## 8 Red fox           Vulpes        14.2  0.0504
## 9 Tiger             Panthera       8.2 NA


# Does `single_sentence` start with "Hello"?
str_starts(single_sentence, "Hello")
## [1] FALSE


# Does `single_sentence` end with a period?
# To search for a literal period, you need to write: "\\." (include two backslashes)
str_ends(single_sentence, "\\.")
## [1] TRUE


# Do strings in `fruits` start with "p"?
str_starts(fruits, "p")
## [1] FALSE  TRUE FALSE  TRUE FALSE FALSE


# Do strings in `fruits` end with "y"?
str_ends(fruits, "y")
## [1] FALSE FALSE  TRUE FALSE  TRUE  TRUE


# Keep only rows where it is `TRUE` that the `genus` ends in "s" dplyr::filter
carnivores %>%
  dplyr::filter(str_ends(genus, "s"))
## # A tibble: 5 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Dog               Canis         13.9  0.07  
## 3 Gray seal         Haliochoerus  17.8  0.325 
## 4 Northern fur seal Callorhinus   15.3 NA     
## 5 Red fox           Vulpes        14.2  0.0504


# Keep only rows where it is `FALSE` that genus starts with "C"
# For this, we negate the result of `str_starts()` using `!` to get `FALSE` instead of `TRUE`
carnivores %>%
  dplyr::filter(!str_starts(genus, "C"))
## # A tibble: 7 × 4
##   name       genus        awake brainwt
##   <chr>      <fct>        <dbl>   <dbl>
## 1 Arctic fox Vulpes        11.5  0.0445
## 2 Cheetah    Acinonyx      11.9 NA     
## 3 Gray seal  Haliochoerus  17.8  0.325 
## 4 Jaguar     Panthera      13.6  0.157 
## 5 Lion       Panthera      10.5 NA     
## 6 Red fox    Vulpes        14.2  0.0504
## 7 Tiger      Panthera       8.2 NA