Compare two strings partially using grepl()
The word “grepl” stands for “grep logical”. The grepl() function in R simply searches for matches in characters or sequences of characters present in a given string.
R
# Define two strings(str1 and str2) String_1 <- "geeks for geeks" String_2 <- "for" # grepl() function to check if "for" is present in or not "geeks for geeks" ans <- grepl (String_2,String_1) # Conditional if statement to check whether ans is true or not if (ans) { cat ( "The strings are equal" ) } else { cat ( "The strings are not equal" ) } |
Output:
The strings are equal
- In this example we check whether “for” is present in “geeks for geeks” string or not.
- Compare them and print the output if it is present i.e. “The strings are equal” or print false i.e. “The strings are not equal”.
Program to Compare two Strings in R
String comparison is a common operation where two strings are compared in a case sensitive manner and check whether they are equal or not. In this article you will learn the various way in which you can compare two string in R language.
Contact Us