str_length
The length of a string.
Technically this returns the number of "code points", in a string. One code point usually corresponds to one character, but not always. For example, an u with a umlaut might be represented as a single character or as the combination a u and an umlaut.
Usage
str_length(string)
Arguments
- string
Input vector. Either a character vector, or something coercible to one.
Value
A numeric vector giving number of characters (code points) in each element of the character vector. Missing string have missing length.
See Also
stringi::stri_length()
which this function wraps.
Examples
# NOT RUN {
str_length(letters)
str_length(NA)
str_length(factor("abc"))
str_length(c("i", "like", "programming", NA))
# Two ways of representing a u with an umlaut
u1 <- "\u00fc"
u2 <- stringi::stri_trans_nfd(u1)
# The print the same:
u1
u2
# But have a different length
str_length(u1)
str_length(u2)
# Even though they have the same number of characters
str_count(u1)
str_count(u2)
# }
Community examples
Going through the doc's examples: ## Mostly same behavior as base::nchar ``` identical(str_length(letters), nchar(letters)) #[1] TRUE identical(str_length(NA), nchar(NA)) #[1] TRUE identical(str_length(c("i", "like", "programming", NA)), nchar(c("i", "like", "programming", NA))) #[1] TRUE u1 <- "\u00fc" u2 <- stringi::stri_trans_nfd(u1) identical(str_length(u1), nchar(u1)) #[1] TRUE identical(str_length(u2), nchar(u2)) #[1] TRUE ``` ## Except when dealing with factors ``` nchar(factor("abc")) # Error in nchar(factor("abc")) : 'nchar()' requires a character vector identical(str_length(factor("abc")), nchar(as.character(factor("abc")))) #[1] TRUE ```