stringr::str_length()
str_length(string) Returns:
integer · Updated March 16, 2026 · Tidyverse stringr string length tidyverse
The str_length() function from stringr returns the number of characters in a string. Unlike base R’s nchar(), this function consistently handles NA values and uses UTF-8 encoding by default, making it more predictable for modern text processing workflows.
Syntax
str_length(string)
Parameters
| Parameter | Type | Description |
|---|---|---|
string | character | A character vector |
Examples
Basic usage
library(stringr)
# Get length of individual strings
str_length("hello")
# [1] 5
# Vectorized over multiple strings
str_length(c("apple", "banana", "cherry"))
# [1] 5 6 6
Handling NA values
# NA is preserved (unlike nchar which may warn)
str_length(c("hello", NA, "world"))
# [1] 5 NA 5
# Compare with base R behavior
nchar(c("hello", NA, "world"))
# [1] 5 NA 6
Working with empty and whitespace strings
# Empty string has zero length
str_length("")
# [1] 0
# Whitespace characters are counted
str_length(" ")
# [1] 2
# Newlines and tabs count as single characters
str_length("a\nb")
# [1] 3
str_length("a\tb")
# [1] 3
Using with dplyr pipelines
library(dplyr)
df <- data.frame(
word = c("apple", "banana", "cherry", "date", "elderberry")
)
df %>%
mutate(char_count = str_length(word))
# word char_count
# 1 apple 5
# 2 banana 6
# 3 cherry 6
# 4 date 4
# 5 elderberry 10
Counting characters in sentences
sentences <- c(
"The quick brown fox.",
"Hello, world!",
"R is great."
)
# Get lengths of each sentence
str_length(sentences)
# [1] 20 13 11
# Find the longest sentence
longest <- sentences[which.max(str_length(sentences))]
longest
# [1] "The quick brown fox."
UTF-8 and special characters
# Emoji count as single characters
str_length("🔥")
# [1] 1
# Combined characters
str_length("é") # Single codepoint
# [1] 1
str_length("e\u0301") # e + combining accent
# [1] 2
# Non-Latin scripts
str_length("日本語")
# [1] 3
Filtering by string length
library(dplyr)
words <- data.frame(
term = c("a", "at", "cat", "cats", "caterpillar")
)
# Keep only words with 3-5 characters
words %>%
filter(between(str_length(term), 3, 5))
# term
# 1 cat
# 2 cats
Common Patterns
- With dplyr::mutate: Add character count columns
- With dplyr::filter: Filter rows by string length
- With dplyr::arrange: Sort by string length
- With max() / min(): Find longest or shortest strings
Summary statistics
text_data <- c("short", "medium length", "this is a longer string", "tiny")
# Character length statistics
c(
min = min(str_length(text_data)),
max = max(str_length(text_data)),
mean = mean(str_length(text_data))
)
# min max mean
# 5 20 11.5
Combined with other stringr functions
# Get only strings longer than 5 characters
words <- c("a", "at", "cat", "cats", "caterpillar")
words[str_length(words) > 5]
# [1] "caterpillar"
# Pad all strings to minimum length
str_pad(words, width = max(str_length(words)), side = "right")
# [1] "a " "at " "cat " "cats " "caterpillar"
See Also
- stringr::str_detect — Detect patterns in strings
- stringr::str_pad — Pad strings to a specified length