Skip to content

R stringr API

stringr — a tidyverse package providing a consistent, pipe-friendly API for string manipulation.

1 class · 8 methods

stringr functions

8 methods

Consistent string manipulation functions from the stringr package, all prefixed with str_.

str_length(string) -> integer

Returns the number of characters (code points) in each string of a character vector.

Parameters

NameTypeDescription
stringcharacterInput character vector.

Returns

integer

Example

r
library(stringr)
str_length(c("hello", "café", ""))  # 5 4 0
str_sub(string, start = 1L, end = -1L) -> character

Extracts substrings by position. Negative indices count from the end.

Parameters

NameTypeDescription
stringcharacterInput character vector.
startintegerStart position (1-based, default 1).
endintegerEnd position (default -1 = last char).

Returns

character

Example

r
library(stringr)
str_sub("Hello, World", 8)      # "World"
str_sub("Hello, World", 8, 12)  # "World"
str_sub("Hello", -3)            # "llo"
str_detect(string, pattern) -> logical

Returns TRUE for each string that matches the pattern, FALSE otherwise.

Parameters

NameTypeDescription
stringcharacterInput character vector.
patterncharacter|regexPattern to match.

Returns

logical

Example

r
library(stringr)
str_detect(c("apple", "banana", "cherry"), "^a")  # TRUE FALSE FALSE
str_detect(c("cat", "dog"), "a")                  # TRUE FALSE
str_replace(string, pattern, replacement) -> character

Replaces the first match of pattern in each string with replacement.

Parameters

NameTypeDescription
stringcharacterInput character vector.
patterncharacter|regexPattern to match.
replacementcharacterReplacement string.

Returns

character

Example

r
library(stringr)
str_replace("Hello, World", "World", "R")  # "Hello, R"
# Use str_replace_all for all matches
str_replace_all("a-b-c", "-", "_")         # "a_b_c"
str_to_lower(string) -> character

Converts strings to lowercase using Unicode rules.

Parameters

NameTypeDescription
stringcharacterInput character vector.

Returns

character

Example

r
library(stringr)
str_to_lower("HELLO World")  # "hello world"
str_split(string, pattern) -> list

Splits each string by pattern, returning a list of character vectors.

Parameters

NameTypeDescription
stringcharacterInput character vector.
patterncharacter|regexSplit pattern.

Returns

list

Example

r
library(stringr)
str_split("a,b,c", ",")
# [[1]]
# [1] "a" "b" "c"
str_split(c("a-b", "c-d-e"), "-")
str_c(..., sep = "") -> character

Concatenates strings element-wise. The stringr equivalent of paste0/paste.

Parameters

NameTypeDescription
...characterStrings to concatenate.
sepcharacterSeparator between inputs (default empty).

Returns

character

Example

r
library(stringr)
str_c("Hello", "World", sep = ", ")  # "Hello, World"
str_c(c("a", "b"), c("1", "2"))      # "a1" "b2"
str_trim(string, side = c("both", "left", "right")) -> character

Trims whitespace from the start, end, or both sides of each string.

Parameters

NameTypeDescription
stringcharacterInput character vector.
sidecharacterWhich side(s) to trim (default both).

Returns

character

Example

r
library(stringr)
str_trim("  hello  ")            # "hello"
str_trim("  hello  ", "left")    # "hello  "
str_trim("  hello  ", "right")   # "  hello"