How to Check if Characters are Present in a String in R.
Last Updated :
24 Apr, 2025
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
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
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
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
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
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
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
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.
Similar Reads
if-else to check if a character string contains a specific substring.
In R Programming Language, we can use the if-else statement to check if a character string contains a specific substring. The if-else statement allows us to execute different code blocks based on a certain condition, in this case, whether the substring is present in the given string. if-else stateme
2 min read
Extract Numbers from Character String Vector in R
In this article, we are going to see how to extract Numbers from Character String Vector in R Programming Language. There are different approaches to extract numbers from character string vectors using some in-built functions. It can be done in the following ways: Using the gsub() functionUsing the
2 min read
R Program to Check if a String is a Palindrome
In this article, we explore a simple yet essential task in programming: checking whether a given string is a palindrome. A palindrome is a sequence of characters that reads the same forwards and backwards, making it a common problem in text processing and string manipulation. We'll delve into the lo
4 min read
Count Number of Characters in String in R
In this article, we are going to see how to get the number of characters in a string in R Programming Language. The number of characters in a string refers to the length of the string. Examples: Input: GeeksforgeeksOutput: 13Explanation: Total 13 characters in the given string. Input: Hello worldOut
2 min read
How to Convert Character to Numeric in R?
In this article, we will discuss how to convert characters to numeric in R Programming Language. We can convert to numeric by using as.numeric() function. Syntax: as.numeric(character) where, character is an character vector Example: C/C++ Code # create a vector with 5 characters data = c('1', '2',
1 min read
Extract First or Last n Characters from String in R
In this article, we will know how to extract the last and first char from a given string in the R programming language. For the example purpose, "Geeks for Geeks is Great" is included in our example string. Let's take a look at how we can extract the first and last n characters of this example strin
4 min read
Remove All White Space from Character String in R
In this article, we are going to see how to remove all the white space from character string in R Programming Language. We can do it in these ways: Using gsub() function.Using str_replace_all() function.Method 1: Using gsub() Function gsub() function is used to remove the space by removing the space
2 min read
Remove All Special Characters from String in R
In this article, we are going to remove all special characters from strings in R Programming language. For this, we will use the str_replace_all() method to remove non-alphanumeric and punctuations which is available in stringr package. Installation To install this library type the below command in
1 min read
Convert Character String to Variable Name in R
In this article we will discuss how to convert a character string to the variable name in the R programming language i.e. we are going to assign the character string to the variable as the variable name Method 1: Using assign() function We can assign character string to variable name by using assign
2 min read
How to convert a DataFrame row into character vector in R?
If we want to turn a dataframe row into a character vector then we can use as.character() method In R, we can construct a character vector by enclosing the vector values in double quotation marks, but if we want to create a character vector from data frame row values, we can use the as character fun
1 min read