Stringr Cheat Sheet

In R

Stringr is a powerful package in R that provides a set of functions for working with strings. It is designed to make string manipulation tasks easier and more efficient. With stringr, you can easily extract, replace, and manipulate strings in your data.

All functions in the package follow a consistent naming convention and syntax, making it easy to learn and use. Additionally, stringr provides a set of regular expression functions that allow you to search for patterns in your strings.

This cheat sheet provides an overview of the most commonly used functions in the stringr package.

String Manipulation Functions

FunctionDescription
str_length()Returns the number of characters in a string.
str_sub()Extracts a substring from a string.
str_trim()Removes leading and trailing whitespace from a string.
str_replace()Replaces a pattern in a string with another pattern.
str_replace_all()Replaces all occurrences of a pattern in a string with another pattern.
str_detect()Returns a logical vector indicating whether a pattern is found in a string.
str_extract()Extracts the first occurrence of a pattern in a string.
str_extract_all()Extracts all occurrences of a pattern in a string.
str_split()Splits a string into a list of substrings based on a delimiter.
str_split_fixed()Splits a string into a matrix of substrings based on a fixed width.

Regular Expression Functions

FunctionDescription
str_view()Displays a string with matches highlighted in a viewer pane.
str_view_all()Displays all matches of a pattern in a string with matches highlighted in a viewer pane.
str_view_regex()Displays a string with matches highlighted in a viewer pane using a regular expression.
str_view_all_regex()Displays all matches of a regular expression in a string with matches highlighted in a viewer pane.
str_extract_regex()Extracts the first occurrence of a regular expression in a string.
str_extract_all_regex()Extracts all occurrences of a regular expression in a string.
str_replace_regex()Replaces a regular expression in a string with another pattern.
str_replace_all_regex()Replaces all occurrences of a regular expression in a string with another pattern.
str_detect_regex()Returns a logical vector indicating whether a regular expression is found in a string.

String Manipulation Examples

library(stringr)

# Create a string
my_string <- ""   Hello, World!   ""

# Remove leading and trailing whitespace
str_trim(my_string)

# Extract a substring
str_sub(my_string, start = 8, end = 13)

# Replace a pattern
str_replace(my_string, pattern = ""o"", replacement = ""a"")

# Replace all occurrences of a pattern
str_replace_all(my_string, pattern = ""o"", replacement = ""a"")

# Check if a pattern is found in a string
str_detect(my_string, pattern = ""World"")

# Extract the first occurrence of a pattern
str_extract(my_string, pattern = ""World"")

# Extract all occurrences of a pattern
str_extract_all(my_string, pattern = ""[a-zA-Z]+"")

# Split a string into a list of substrings
str_split(my_string, pattern = "","")

# Split a string into a matrix of substrings
str_split_fixed(my_string, width = 2)

Regular Expression Examples

library(stringr)

# Create a string
my_string <- ""The quick brown fox jumps over the lazy dog.""

# View the string with matches highlighted
str_view(my_string, pattern = ""o"")

# View all matches of a pattern in a string
str_view_all(my_string, pattern = ""o"")

# View the string with matches highlighted using a regular expression
str_view_regex(my_string, pattern = ""\\b[a-z]{4}\\b"")

# View all matches of a regular expression in a string
str_view_all_regex(my_string, pattern = ""\\b[a-z]{4}\\b"")

# Extract the first occurrence of a regular expression in a string
str_extract_regex(my_string, pattern = ""\\b[a-z]{4}\\b"")

# Extract all occurrences of a regular expression in a string
str_extract_all_regex(my_string, pattern = ""\\b[a-z]{4}\\b"")

# Replace a regular expression in a string with another pattern
str_replace_regex(my_string, pattern = ""\\b[a-z]{4}\\b"", replacement = ""****"")

# Replace all occurrences of a regular expression in a string with another pattern
str_replace_all_regex(my_string, pattern = ""\\b[a-z]{4}\\b"", replacement = ""****"")

# Check if a regular expression is found in a string
str_detect_regex(my_string, pattern = ""\\b[a-z]{4}\\b"")

For more information on the stringr package, see the official documentation.