if-else to check if a character string contains a specific substring. Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 statementThe if-else statement is a control structure in R used to make decisions based on a given condition. Character stringsThe if-else statement is a control structure in R used to make decisions based on a given condition. SubstringA substring is a smaller sequence of characters that appears within a larger character string. To check if a character string contains a specific substring, follow these steps: Define the main character string.Define the substring you want to check for.Use the if-else statement to check if the substring is present in the main string.Execute different code blocks based on the presence or absence of the substring.Let's consider a few examples to illustrate how to use the if-else statement in R to check for a specific substring. Example 1: Checking if a string is present in the string R # Step 1: Define the main character string main_string <- "I have an apple and a banana." # Step 2: Define the substring to check for substring_to_check <- "apple" # Step 3: Use the if-else statement to check if the substring is present if (grepl(substring_to_check, main_string)) { # Step 4a: If the substring is present, execute this block of code print("The string contains 'apple'.") } else { # Step 4b: If the substring is not present, execute this block of code print("The string does not contain 'apple'.") } Output: [1] "The string contains 'apple'."Example 2: Checking if "mango" is present in the string R # Step 1: Define the main character string main_string <- "We are enjoying delicious fruits." # Step 2: Define the substring to check for substring_to_check <- "mango" # Step 3: Use the if-else statement to check if the substring is present if (grepl(substring_to_check, main_string)) { # Step 4a: If the substring is present, execute this block of code print("The string contains 'mango'.") } else { # Step 4b: If the substring is not present, execute this block of code print("The string does not contain 'mango'.") } Output: [1] "The string does not contain 'mango'." Comment More infoAdvertise with us Next Article if-else to check if a character string contains a specific substring. lunatic1 Follow Improve Article Tags : R Programs Similar Reads How to Check if Characters are Present in a String in R. 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 ch 6 min read 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. Concepts Related to the Topic :String Comparison: Comparison of 4 min read PHP to Check if a String Contains any Special Character Given a String, the task is to check whether a string contains any special characters in PHP. Special characters are characters that are not letters or numbers, such as punctuation marks, symbols, and whitespace characters. Examples: Input: str = "Hello@Geeks"Output: String contain special character 3 min read How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri 1 min read Check If An Array of Strings Contains a Substring in JavaScript Here are the different ways to check if an array of strings contains a substring in JavaScript1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.JavaScriptconst a = ['Geeks', 'gf 4 min read JavaScript - Check if a String Contains Any Digit Characters Here are the various methods to check if a string contains any digital character1. Using Regular Expressions (RegExp)The most efficient and popular way to check if a string contains digits is by using a regular expression. The pattern \d matches any digit (0-9), and with the test() method, we can ea 4 min read How to Check if a Substring Exists in a Char Array in C++? In C++, Char array and std::string both are used to store a sequence of characters to represent textual data. In this article, we will learn how to check if a substring exists within a char array in C++. Example Input: charArray[]= "Hello, Geek" subString="Geek" Output: Geek substring is found in ch 2 min read Python program to check a string for specific characters Here, will check a string for a specific character using different methods using Python. In the below given example a string 's' and char array 'arr', the task is to write a python program to check string s for characters in char array arr. Examples: Input: s = @geeksforgeeks% arr[] = {'o','e','%'}O 4 min read Extract a Substring Between Two Characters in a String in Java In Java, the extraction of the substring between the two characters in a string can be implemented with the substring method, and string is a pre-defined method in the class of the String. A String is a pre-defined class of the java.lang package. substring(startIndex, endIndex): This is a pre-define 2 min read How to check a string starts/ends with a specific string in PHP ? In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP.We require the use of XAMPP, Netbeans to run this code.Input: "Geeks For Geeks"Starting with: "G"Output: "Geeks For Geeks Starts with String G"In PHP version 8+, there ar 2 min read Like