String Length in R
In this tutorial, we will learn how to find string length in R programming.
To find the length of a String in R, use nchar() function. Following is the syntax of nchar function.
nchar(x)
where x is of character data type
The nchar() function counts the number of characters in each element of a character vector. Spaces, punctuation marks, and digits are counted as characters. For example, the string 'Hello World!' has 12 characters because the space and exclamation mark are included.
nchar() syntax for counting characters in R strings
The basic use is nchar(x). In practical R programs, you may also see optional arguments such as type, allowNA, and keepNA. For most beginner examples, the default call is enough.
nchar(x, type = "chars", allowNA = FALSE, keepNA = NA)
x: character vector, number, factor, or another value that can be treated as character text.type = "chars": counts characters. This is the usual choice for string length.type = "bytes": counts bytes, which can differ from characters for some Unicode text.keepNA: controls how missing values are handled.
Example 1 – Find Length of String in R
In this example, we will initialize a variable with a String value and find its length using nchar(string) function.
r_strings_length.R
# Example R program to find length of string
str = 'Hello World!'
# find length using nchar(c)
length = nchar(str)
print (length)
Output
$ Rscript r_strings_length.R
[1] 12
The result is 12 because the text contains 10 letters, 1 blank space, and 1 exclamation mark.
Example 2 – Find Length of a Number in R
nchar automatically type casts number to character
r_strings_length.R
# Example R program to find length of number
num = 523641
# find length using nchar(c)
length = nchar(num)
print (length)
Output
$ Rscript r_strings_length.R
[1] 6
Here, the number 523641 is treated like the character text "523641", so the count is 6.
You may try nchar(c) function for other data types to find the length.
Find the length of each string in an R character vector
If x is a character vector, nchar() returns one count for each element. This is useful when checking names, codes, labels, or any column of text values.
words <- c("R", "Data", "TutorialKart", "Hello World")
result <- nchar(words)
print(result)
Output
[1] 1 4 12 11
The fourth value is 11 because "Hello World" has 10 letters and 1 space.
nchar() with empty strings, spaces, and missing values in R
An empty string has length 0. A string that contains only a space has length 1. Missing values need separate attention because NA means the value itself is not available.
values <- c("", " ", "R", NA)
print(nchar(values))
Output
[1] 0 1 1 NA
This result shows why it is useful to distinguish between an empty string "", a blank-space string " ", and a missing value NA.
Difference between nchar() and length() for R strings
Use nchar() when you want the number of characters inside a string. Use length() when you want the number of elements in an R object, such as the number of items in a vector.
| Expression | What it counts | Example result |
|---|---|---|
nchar("Hello") | Characters inside the string | 5 |
length("Hello") | Elements in the character vector | 1 |
nchar(c("Hi", "World")) | Characters in each vector element | 2 5 |
length(c("Hi", "World")) | Number of vector elements | 2 |
text <- "Hello"
items <- c("Hi", "World")
print(nchar(text))
print(length(text))
print(nchar(items))
print(length(items))
Output
[1] 5
[1] 1
[1] 2 5
[1] 2
So, length() is not a replacement for nchar() when the task is to find the number of characters in R text.
Count characters after removing leading and trailing spaces in R
Sometimes a string contains extra spaces at the beginning or end. If those spaces should not be counted, remove them with trimws() before calling nchar().
name <- " R Tutorial "
print(nchar(name))
print(nchar(trimws(name)))
Output
[1] 14
[1] 10
The first count includes the two spaces before and the two spaces after the text. The second count uses the trimmed string.
Using stringr str_length() as an alternative in R
In base R, nchar() is the standard function for string length. In tidyverse-style code, you may also see str_length() from the stringr package. It returns the number of characters in each string, similar to nchar() for common string-length tasks.
# install.packages("stringr")
library(stringr)
text <- c("R", "Hello")
print(str_length(text))
Output
[1] 1 5
If you are learning basic R, start with nchar(). Use str_length() when your project already uses the stringr package for other string operations.
Common mistakes when finding string length in R
- Do not use
length()to count characters inside one string; it counts vector elements. - Remember that spaces and punctuation are included in the count.
- Check whether extra leading or trailing spaces should be counted before using the result.
- Handle
NAvalues separately if missing text values are possible in your data. - Avoid naming variables
lengthin larger scripts becauselength()is also a built-in R function name.
Editorial QA checklist for R string length examples
- Confirm that every example uses
nchar()for character count andlength()only for vector element count. - Verify counts manually when examples include spaces, punctuation, or empty strings.
- Keep output blocks separate from R code blocks so readers can distinguish code from results.
- Explain whether blank spaces are included or removed in each example.
- Use
stringr::str_length()only when the example clearly needs the stringr package.
Frequently asked questions about R string length
How do I get the length of a string in R?
Use nchar(). For example, nchar("Hello") returns 5.
Is it length() or nchar() for strings in R?
Use nchar() to count characters in a string. Use length() to count the number of elements in an R object such as a vector.
Does nchar() count spaces in R?
Yes. nchar() counts spaces as characters. For example, nchar("A B") returns 3 because the space between A and B is included.
How do I find string length after trimming spaces in R?
Use trimws() first and then call nchar(). For example, nchar(trimws(" R ")) returns 1.
Can nchar() count the length of numbers in R?
Yes. nchar() can count digits after treating the number as text. For example, nchar(523641) returns 6.
Conclusion
In this R Tutorial, we have learned to use nchar(x) to find the string length or number length in R programming language. We also saw how nchar() works with vectors, empty strings, spaces, missing values, trimmed strings, and how it differs from length().
TutorialKart.com