To check if a string is empty in R, you can use the nchar
function to get the number of characters in the string.
In this example,
str1
with an empty string value ""
.nchar
function on str1
to get the number of characters.0
, the string is empty.str1 <- ""
isEmpty <- nchar(str1) == 0
print(paste("Is string empty:", isEmpty))
[1] "Is string empty: TRUE"
In this example,
str2
with a non-empty string value "Hello World"
.nchar
function on str2
to get the number of characters.0
, the string is empty.str2 <- "Hello World"
isEmpty <- nchar(str2) == 0
print(paste("Is string empty:", isEmpty))
[1] "Is string empty: FALSE"
In this tutorial, we learned How to Check if String is Empty in R language with well detailed examples.