To check if a string contains only alphabets and numbers in R, you can use the grepl
function with a regular expression pattern.
In this example,
grepl
function to check if the string matches the pattern.containsOnlyAlphabetsAndNumbers
will be TRUE
, otherwise FALSE
.str <- "Hello123"
pattern <- "^[[:alnum:]]+\$"
containsOnlyAlphabetsAndNumbers <- grepl(pattern, str)
print(paste("String contains only alphabets and numbers:", containsOnlyAlphabetsAndNumbers))
String contains only alphabets and numbers: TRUE
In this tutorial, we learned How to Check if String contains only Alphabets and Numbers in R language with well detailed examples.