Check if Substring is in String Google Sheets

admin17 March 2024Last Update :

Check if Substring is in String Google Sheets

Google Sheets is a powerful tool for organizing and analyzing data. One common task that users often need to perform is checking if a substring is present in a string. This can be useful for various purposes, such as filtering data or performing conditional calculations. In this article, we will explore different methods to check if a substring is in a string in Google Sheets, along with examples and practical use cases.

Method 1: Using the FIND function

The FIND function in Google Sheets allows you to search for a specific substring within a larger string. It returns the position of the substring within the string, or an error if the substring is not found. To check if a substring is in a string, you can use the FIND function in combination with an IF statement.

=IF(ISNUMBER(FIND("substring", A1)), "Substring found", "Substring not found")

In the above formula, “substring” is the substring you want to search for, and A1 is the cell containing the string you want to search in. If the FIND function returns a number (indicating the position of the substring), the ISNUMBER function will return TRUE, and the IF statement will display “Substring found”. Otherwise, it will display “Substring not found”.

Here’s an example to illustrate this:

String Substring Result
Google Sheets is awesome Sheets Substring found
Google Sheets is awesome Excel Substring not found

Method 2: Using the SEARCH function

The SEARCH function is similar to the FIND function, but it is case-insensitive. This means that it will find the substring regardless of the case of the letters. The syntax of the SEARCH function is the same as the FIND function:

=IF(ISNUMBER(SEARCH("substring", A1)), "Substring found", "Substring not found")

Here’s an example:

String Substring Result
Google Sheets is awesome Sheets Substring found
Google Sheets is awesome sheets Substring found

Method 3: Using the REGEXMATCH function

If you need more advanced pattern matching capabilities, you can use the REGEXMATCH function in Google Sheets. This function allows you to check if a string matches a regular expression pattern. To check if a substring is in a string, you can use the REGEXMATCH function with a regular expression pattern that matches the substring.

=IF(REGEXMATCH(A1, ".*substring.*"), "Substring found", "Substring not found")

In the above formula, “substring” is the substring you want to search for, and A1 is the cell containing the string you want to search in. The regular expression pattern “.*substring.*” matches any string that contains the substring.

Here’s an example:

String Substring Result
Google Sheets is awesome Sheets Substring found
Google Sheets is awesome Excel Substring not found

Method 4: Using custom formulas

If the built-in functions in Google Sheets don’t meet your specific requirements, you can create a custom formula using Google Apps Script. Google Apps Script allows you to extend the functionality of Google Sheets by writing JavaScript code.

Here’s an example of a custom formula that checks if a substring is in a string:

function SUBSTRING_IN_STRING(string, substring) {
  return string.indexOf(substring) !== -1;
}

To use this custom formula, you need to follow these steps:

  • Open your Google Sheets document.
  • Click on “Extensions” in the menu bar.
  • Select “Apps Script”.
  • In the Apps Script editor, paste the above code.
  • Save the script.

Once you have saved the script, you can use the custom formula in your Google Sheets document like any other built-in function:

=SUBSTRING_IN_STRING(A1, "substring")

This will return TRUE if the substring is found in the string, and FALSE otherwise.

FAQ Section

Q: Can I use wildcards in the substring?

A: Yes, you can use wildcards in the substring when using the REGEXMATCH function. Regular expressions allow you to define complex patterns for matching strings. For example, the regular expression “.*substring.*” matches any string that contains the substring, regardless of its position within the string.

Q: Are the FIND and SEARCH functions case-sensitive?

A: The FIND function is case-sensitive, while the SEARCH function is case-insensitive. This means that the FIND function will only find an exact match of the substring, including the case of the letters. On the other hand, the SEARCH function will find the substring regardless of the case of the letters.

Q: Can I check if multiple substrings are in a string?

A: Yes, you can check if multiple substrings are in a string by using the methods described above in combination with logical operators. For example, you can use the following formula to check if both “substring1” and “substring2” are present in the string:

=IF(AND(ISNUMBER(FIND("substring1", A1)), ISNUMBER(FIND("substring2", A1))), "Both substrings found", "One or both substrings not found")

This formula uses the FIND function and the AND function to check if both substrings are found. If they are, it displays “Both substrings found”. Otherwise, it displays “One or both substrings not found”.

Conclusion

Checking if a substring is in a string is a common task in Google Sheets. By using the FIND, SEARCH, and REGEXMATCH functions, as well as custom formulas, you can easily perform this task and incorporate it into your data analysis workflows. Whether you need to filter data, perform conditional calculations, or extract specific information, these methods will help you efficiently work with substrings in Google Sheets.

References:

Leave a Comment

Your email address will not be published. Required fields are marked *


Comments Rules :

Breaking News