406

How to find the length of a string (i.e., number of characters in a string) without splitting it in R? I know how to find the length of a list but not of a string.

And what about Unicode strings? How do I find the length (in bytes) and the number of characters (runes, symbols) in a Unicode string?

Related Question:

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
  • 1
    using evaluate() along with an anonymous function to return the last element of | the vector c(8, 4, 0). Your anonymous function should only take one argument which should | be a variable `x`. – uxi Feb 04 '17 at 14:25

6 Answers6

489

See ?nchar. For example:

> nchar("foo")
[1] 3
> set.seed(10)
> strn <- paste(sample(LETTERS, 10), collapse = "")
> strn
[1] "NHKPBEFTLY"
> nchar(strn)
[1] 10
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
76

Use stringi package and stri_length function

> stri_length(c("ala ma kota","ABC",NA))
[1] 11  3 NA

Why? Because it is the FASTEST among presented solutions :)

require(microbenchmark)
require(stringi)
require(stringr)
x <- c(letters,NA,paste(sample(letters,2000,TRUE),collapse=" "))
microbenchmark(nchar(x),str_length(x),stri_length(x))
Unit: microseconds
           expr    min     lq  median      uq     max neval
       nchar(x) 11.868 12.776 13.1590 13.6475  41.815   100
  str_length(x) 30.715 33.159 33.6825 34.1360 173.400   100
 stri_length(x)  2.653  3.281  4.0495  4.5380  19.966   100

and also works fine with NA's

nchar(NA)
## [1] 2
stri_length(NA)
## [1] NA

EDIT 2021

NA argument is no longer valid if you are using latest R version.

bartektartanus
  • 15,284
  • 6
  • 74
  • 102
30

You could also use the stringr package:

library(stringr)
str_length("foo")
[1] 3
johannes
  • 14,043
  • 5
  • 40
  • 51
28
nchar("STRING")

Check out this

sobingt
  • 402
  • 4
  • 6
13

The keepNA = TRUE option prevents problems with NA

nchar(NA)
## [1] 2
nchar(NA, keepNA=TRUE)
## [1] NA
Thomas Buhl
  • 303
  • 2
  • 7
  • 1
    As of 3.3.1 the base defaults are set to give `nchar(NA) ## [1] NA` : see [nchar RDocumentation](https://www.rdocumentation.org/packages/base/versions/3.3.1/topics/nchar) – leerssej Oct 11 '16 at 01:12
9
nchar(YOURSTRING)

you may need to convert to a character vector first;

nchar(as.character(YOURSTRING))
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Jonathan
  • 99
  • 1
  • 1
  • 1
    With the exception of a factor input, the coercion is performed by `nchar`. For factor inputs, `nchar` will throw an error and hence you will need to do the conversion first as you show. – Gavin Simpson Mar 08 '13 at 16:07