To get the length of a given string in R language, you can use the nchar()
function.
In this example,
str1
with a string value of "Hello World"
.nchar()
function to get the length of the string and store the returned value in the variable length1
.nchar()
function returns the length of this string.length1
to the console using cat()
function.# Length of "Hello World" String
str1 <- "Hello World"
length1 <- nchar(str1)
cat("Length of given string is:", length1, "\n")
Length of given string is: 11
In this example,
str2
with a string value of "apple"
.nchar()
function to get the length of the string and store the returned value in the variable length2
.nchar()
function returns the length of this string.length2
to the console using cat()
function.# Length of "apple" String
str2 <- "apple"
length2 <- nchar(str2)
cat("Length of given string is:", length2, "\n")
Length of given string is: 5
In this tutorial, we learned How to get Length of String in R language with well detailed examples.