if-else to check if a character string contains a specific substring. Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share 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 How to check a string starts/ends with a specific string in PHP ? L lunatic1 Follow Improve Article Tags : R Language Similar Reads 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 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 How to check a string starts/ends with a specific string in jQuery ? JavaScript provides a lot of string methods that check whether a string is a substring of another string. So, jQuery wouldn't be necessary at all to perform this task. However, we will cover all the different ways of checking whether a string starts or ends with a string:Â startsWith() and endsWith() 5 min read Shell Script To Check That Input Only Contains Alphanumeric Characters If you want an input which contains only alphanumeric characters i.e. 1-9 or a-z lower case as well as upper case characters, We can make use of regular expression or Regex in short in a Shell Script to verify the input. Example: Input: Geeksforgeeks Output: True Explanation: Here all the inputted d 2 min read JavaScript - How to Check if a String "StartsWith" Another String in JavaScript? Here are the various methods to check if a string starts with another string in JavaScript1. Using String.startsWith() MethodThe most widely used method is the startsWith() method, built into JavaScript. This method is simple to use, highly readable, and performs the task efficiently.JavaScriptlet s 3 min read Like