How to calculate the size of a string using nchar() in R

Share

The nchar() function in R is used to calculate the size of a string.

The illustration below shows the mathematical representation of the nchar() function.

Visual representation of nchar() function

Syntax

nchar(string)

Parameter

The nchar() function requires a string as a parameter.

Return value

This function returns the size of a string.

Code

a <- nchar("hello");
print(paste0("nchar('hello): ", a))
b <- nchar("educative");
print(paste0("nchar('educative): ", b))