Open In App

How to Check if Characters are Present in a String in R.

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to check the presence of a character, substring or number in a string using R Programming Language. This can be useful for tasks where we want to filter data, pattern matching or data cleaning. We generally use grepl() function, a versatile tool that is used for checking patterns or characters in strings. We'll search for both case-sensitive and case-insensitive manner, and below are a few examples in which you can learn how to check if characters are present in a string using R language.

Concepts Related to the Topic

  • String: A string is a set of characters or sequences of characters stored in a contiguous memory location.
  • grepl("[A-Za-z]", str): This function will check the presence of an alphabetic character in a string.
  • grepl("[0-9]" , string): This function will check the presence of a numeric value in a string.
  • grepl("[^A-Za-z0-9 ]", str) : This function will check the presence of a special character in a string.
  • grepl("[A-Za-z0-9]", str): This function will check the presence of alphabetic characters as well as numeric values in a string.
  • grepl("substring", string): The grepl() function is commonly used to check for if a specific pattern or a substring or a number is present in a string or not. It returns a logical vector indicating whether it was able to find that substring or not.
  • grepl("substring", string, ignore.case = TRUE): This function will check the presence of a substring in a case-sensitive manner.

General steps needed

To check the presence of characters in a string in R language, follow the below steps:

  • Define a string in which we want to search a substring.
  • We use grepl() function to check for the presence of characters or substrings.
  • Return TRUE if we found a character or substring, else return FALSE.
  • Then print the desired result.

Checking for Alphabetic Characters in a string

R
# Define the string
str 

Output:

Alphabets present

In this example we define a string "geeksforgeeks".

  • Now we apply if condition to check the presence of Alphabetic Characters in a string using grepl("[A-Za-z]", str) function.
  • Since alphabets are present in the string "geeksforgeeks" so if condition will return TRUE and print "Alphabets present".

Checking digits in a string

R
# Define the string
str 

Output:

'geeksforgeeks_10' contains a digit

In this example we define a string "geeksforgeeks_10".

  • Then we apply if condition to check the presence of numeric value from [0-9] is present in the the define string or not.
  • Since "10" is present in the string "geeksforgeeks_10" so if condition will return TRUE and print "geeksforgeeks_10 contains a digit".

Checking for Special Characters in a string

R
# Define the string
str 

Output:

special characters present

In this example we define a string "@geeksforgeeks".

  • Now we apply if condition to check the presence of special Characters in a string using grepl("[^A-Za-z0-9 ]", str) function.
  • Since special character is present in the string "@geeksforgeeks" so if condition will return TRUE and print "special character present".

Checking for Alphanumeric Characters in a string

R
# Define the string
str 

Output:

alphanumeric characters present

In this example we define a string "@geeksforgeeks".

  • Now we apply if condition to check the presence of Alphanumeric Characters in a string using grepl("[^A-Za-z0-9 ]", str) function.
  • Since special character i.e. '@' is present in the string "@geeksforgeeks" so if condition will return TRUE and print "special character present".

Using grepl() to check the presence of substring

R
# Define the string
str 

Output:

'for' is present in geeksforgeeks

In this example we define a string as "geeksforgeeks".

  • Then we apply the if condition to check the presence of "for" substring in "geeksforgeeks" string.
  • Since "for" is present in "geeksforgeeks" so it will return TRUE and print "for is present in geeksforgeeks" using cat statement.

Case-Insensitive Search

R
# Define the string
str 

Output:

'FOR' is present in GeeksForGeeks

In this example we define a string "GeeksForGeeks".

  • Then we apply if condition to check the presence of "For" substring in "GeeksForGeeks" string.
  • We have to check in case sensitive manner so we use grepl("FOR", str, ignore.case = TRUE) function which will ignore the upper and lower case and check the presence of 'FOR' in "GeeksForGeeks".
  • Since 'FOR' is present in the string so, if condition will return TRUE and print "FOR is present in GeeksForGeeks" using cat statement.

Checking for Multiple Substrings

R
# Define the string
str 

Output:

Either 'R' or 'is' is present in the string

In this example we define a string "R programming is fun!".

  • Then we apply if condition to check if "R" or "is" is present in the string using | operator which indicate logical OR operation.
  • grepl("R | is", str) function will check whether 'R' or 'is' is present in the string or not.
  • Since 'R' and 'is' both are present in the string so, if condition will return TRUE and print "Either 'R' or 'is' is present in the string"using cat statement.

Conclusion

These are the few examples in which you have learnt how to check the presence of a character in a string using R language.


Next Article

Similar Reads