stringr functions
8 methodsConsistent string manipulation functions from the stringr package, all prefixed with str_.
str_length(string) -> integerReturns the number of characters (code points) in each string of a character vector.
Parameters
| Name | Type | Description |
|---|---|---|
| string | character | Input character vector. |
Returns
integer
Example
library(stringr)
str_length(c("hello", "café", "")) # 5 4 0str_sub(string, start = 1L, end = -1L) -> characterExtracts substrings by position. Negative indices count from the end.
Parameters
| Name | Type | Description |
|---|---|---|
| string | character | Input character vector. |
| start | integer | Start position (1-based, default 1). |
| end | integer | End position (default -1 = last char). |
Returns
character
Example
library(stringr)
str_sub("Hello, World", 8) # "World"
str_sub("Hello, World", 8, 12) # "World"
str_sub("Hello", -3) # "llo"str_detect(string, pattern) -> logicalReturns TRUE for each string that matches the pattern, FALSE otherwise.
Parameters
| Name | Type | Description |
|---|---|---|
| string | character | Input character vector. |
| pattern | character|regex | Pattern to match. |
Returns
logical
Example
library(stringr)
str_detect(c("apple", "banana", "cherry"), "^a") # TRUE FALSE FALSE
str_detect(c("cat", "dog"), "a") # TRUE FALSEstr_replace(string, pattern, replacement) -> characterReplaces the first match of pattern in each string with replacement.
Parameters
| Name | Type | Description |
|---|---|---|
| string | character | Input character vector. |
| pattern | character|regex | Pattern to match. |
| replacement | character | Replacement string. |
Returns
character
Example
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) -> characterConverts strings to lowercase using Unicode rules.
Parameters
| Name | Type | Description |
|---|---|---|
| string | character | Input character vector. |
Returns
character
Example
library(stringr)
str_to_lower("HELLO World") # "hello world"str_split(string, pattern) -> listSplits each string by pattern, returning a list of character vectors.
Parameters
| Name | Type | Description |
|---|---|---|
| string | character | Input character vector. |
| pattern | character|regex | Split pattern. |
Returns
list
Example
library(stringr)
str_split("a,b,c", ",")
# [[1]]
# [1] "a" "b" "c"
str_split(c("a-b", "c-d-e"), "-")str_c(..., sep = "") -> characterConcatenates strings element-wise. The stringr equivalent of paste0/paste.
Parameters
| Name | Type | Description |
|---|---|---|
| ... | character | Strings to concatenate. |
| sep | character | Separator between inputs (default empty). |
Returns
character
Example
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")) -> characterTrims whitespace from the start, end, or both sides of each string.
Parameters
| Name | Type | Description |
|---|---|---|
| string | character | Input character vector. |
| side | character | Which side(s) to trim (default both). |
Returns
character
Example
library(stringr)
str_trim(" hello ") # "hello"
str_trim(" hello ", "left") # "hello "
str_trim(" hello ", "right") # " hello"