This is an old question but a solution for those using Excel 2016 or newer is you can remove the need for nested if structures by using the new IFS( condition1, return1 [,condition2, return2] ...)
conditional.
I have formatted it to make it visually clearer on how to use it for the case of this question:
=IFS(
ISERROR(SEARCH("String1",A1))=FALSE,"Something1",
ISERROR(SEARCH("String2",A1))=FALSE,"Something2",
ISERROR(SEARCH("String3",A1))=FALSE,"Something3"
)
Since SEARCH
returns an error if a string is not found I wrapped it with an ISERROR(...)=FALSE
to check for truth and then return the value wanted. It would be great if SEARCH
returned 0 instead of an error for readability, but thats just how it works unfortunately.
Another note of importance is that IFS
will return the match that it finds first and thus ordering is important. For example if my strings were Surf, Surfing, Surfs
as String1,String2,String3
above and my cells string was Surfing
it would match on the first term instead of the second because of the substring being Surf
. Thus common denominators need to be last in the list. My IFS
would need to be ordered Surfing, Surfs, Surf
to work correctly (swapping Surfing
and Surfs
would also work in this simple example), but Surf
would need to be last.
In this article, we will learn how to search a substring from a given string in Excel.
In excel, substring is a part of another string. It can be a single character or a whole paragraph. To search a string for a specific substring, we will use the ISNUMBER function along with the FIND function in Excel. Instead of FIND, you can always use the excel SEARCH function for non-case sensitive searches.
ISNUMBER function is used to check the cell if it contains a number or not.
Syntax of ISNUMBER:
The FIND function returns the position of the character in a text string, reading left to right (case-sensitive).
Syntax of Find:
=FIND(find_text,within_text,[start_num])
Here we have two columns. Substring in Column B and Given string in Column A.
Write the formula in C2 cell
Formula:
Explanation:
Find function takes the substring from the B2 cell of Column B and it then matches it with the given string in the A2 cell of Column A.
ISNUMBER checks if the string matches, it returns True else it returns False.
Copy the formula in other cells, select the cells taking the first cell where the formula is already applied, use shortcut key Ctrl+ D.
As you can see the output in column C shows True and False representing whether substring is there or not.
Now, what if you want to do a non-case sensitive search in excel?
How to search in excel cells for non-case sensitive substrings?
It’s simple. Replace the find function with the excel SEARCH function in the formula above. Since SEARCH is case-insensitive.
There are more articles on FIND and SEARCH function to find strings or value. Hope you learned how to find substring in a string from the article. You can perform these tasks in Excel 2016, 2013 and 2010. If you have any unresolved query, please do comment below in the comment box. We will help you.
Related Articles:
How to Search a certain text in Excel
How to Sum if cells contain specific text in Excel
How to Sum if cell contains text in other cell in Excel
How to Count Cells that contain specific text in Excel
How to Split Numbers and Text from String in Excel
How to Highlight cells that contain specific text in Excel
Popular Articles:
50 Excel Shortcut to Increase Your Productivity
How to use the VLOOKUP Function in Excel
How to use the COUNTIF function in Excel 2016
How to use the SUMIF Function in Excel
In this example, the goal is to test a value in a cell to see if it contains a specific substring. Excel contains two functions designed to check the occurrence of one text string inside another: the SEARCH function and the FIND function. Both functions return the position of the substring if found as a number, and a #VALUE! error if the substring is not found. The difference is that the SEARCH function supports wildcards but is not case-sensitive, while the FIND function is case-sensitive but does not support wildcards. The general approach with either function is to use the ISNUMBER function to check for a numeric result (a match) and return TRUE or FALSE.
SEARCH function (not case-sensitive)
The SEARCH function is designed to look inside a text string for a specific substring. If SEARCH finds the substring, it returns a position of the substring in the text as a number. If the substring is not found, SEARCH returns a #VALUE error. For example:
=SEARCH("p","apple") // returns 2
=SEARCH("z","apple") // returns #VALUE!
To force a TRUE or FALSE result, we use the ISNUMBER function. ISNUMBER returns TRUE for numeric values and FALSE for anything else. So, if SEARCH finds the substring, it returns the position as a number, and ISNUMBER returns TRUE:
=ISNUMBER(SEARCH("p","apple")) // returns TRUE
=ISNUMBER(SEARCH("z","apple")) // returns FALSE
If SEARCH doesn’t find the substring, it returns an error, which causes the ISNUMBER to return FALSE.
Wildcards
Although SEARCH is not case-sensitive, it does support wildcards (*?~). For example, the question mark (?) wildcard matches any one character. The formula below looks for a 3-character substring beginning with «x» and ending in «y»:
=ISNUMBER(SEARCH("x?z","xyz")) // TRUE
=ISNUMBER(SEARCH("x?z","xbz")) // TRUE
=ISNUMBER(SEARCH("x?z","xyy")) // FALSE
The asterisk (*) wildcard matches zero or more characters. This wildcard is not as useful in the SEARCH function because SEARCH already looks for a substring. For example, it might seem like the following formula will test for a value that ends with «z»:
=ISNUMBER(SEARCH("*z",text))
However, because SEARCH automatically looks for a substring, the following formulas all return 1 as a result, even though the text in the first formula is the only text that ends with «z»:
=SEARCH("*z","XYZ") // returns 1
=SEARCH("*z","XYZXY") // returns 1
=SEARCH("*z","XYZXY123") // returns 1
=SEARCH("x*z","XYZXY123") // returns 1
This means the asterisk (*) is not a reliable way to test for «ends with». However, you an use the the asterisk (*) wildcard like this:
=SEARCH("x*2*b","AAAXYZ123ABCZZZ") // returns 4
=SEARCH("x*2*b","NXYZ12563JKLB") // returns 2
Here we are looking for «x», «2», and «b» in that order, with any number of characters in between. Finally, you can use the tilde (~) as an escape character to indicate that the next character is a literal like this:
=SEARCH("~*","apple*") // returns 6
=SEARCH("~?","apple?") // returns 6
=SEARCH("~~","apple~") // returns 6
The above formulas use SEARCH to find a literal asterisk (*), question mark (?) , and tilde (~) in that order.
FIND function (case-sensitive)
Like the SEARCH function, the FIND function returns the position of a substring in text as a number, and an error if the substring is not found. However, unlike the SEARCH function, the FIND function respects case:
=FIND("A","Apple") // returns 1
=FIND("A","apple") // returns #VALUE!
To make a case-sensitive version of the formula, just replace the SEARCH function with the FIND function in the formula above:
=ISNUMBER(FIND(substring,A1))
The result is a case-sensitive search:
=ISNUMBER(FIND("A","Apple")) // returns TRUE
=ISNUMBER(FIND("A","apple")) // returns FALSE
If cell contains
To return a custom result when a cell contains specific text, add the IF function like this:
=IF(ISNUMBER(SEARCH(substring,A1)), "Yes", "No")
Instead of returning TRUE or FALSE, the formula above will return «Yes» if substring is found and «No» if not.
With hardcoded search string
To test for a hardcoded substring, enclose the text in double quotes («»). For example, to check A1 for the text «apple» use:
=ISNUMBER(SEARCH("apple",A1))
More than one search string
To test a cell for more than one thing (i.e. for one of many substrings), see this example formula.
To check if a cell contains specific text, use ISNUMBER and SEARCH in Excel. There’s no CONTAINS function in Excel.
1. To find the position of a substring in a text string, use the SEARCH function.
Explanation: «duck» found at position 10, «donkey» found at position 1, cell A4 does not contain the word «horse» and «goat» found at position 12.
2. Add the ISNUMBER function. The ISNUMBER function returns TRUE if a cell contains a number, and FALSE if not.
Explanation: cell A2 contains the word «duck», cell A3 contains the word «donkey», cell A4 does not contain the word «horse» and cell A5 contains the word «goat».
3. You can also check if a cell contains specific text, without displaying the substring. Make sure to enclose the substring in double quotation marks.
4. To perform a case-sensitive search, replace the SEARCH function with the FIND function.
Explanation: the formula in cell C3 returns FALSE now. Cell A3 does not contain the word «donkey» but contains the word «Donkey».
5. Add the IF function. The formula below (case-insensitive) returns «Found» if a cell contains specific text, and «Not Found» if not.
6. You can also use IF and COUNTIF in Excel to check if a cell contains specific text. However, the COUNTIF function is always case-insensitive.
Explanation: the formula in cell C2 reduces to =IF(COUNTIF(A2,»*duck*»),»Found»,»Not Found»). An asterisk (*) matches a series of zero or more characters. Visit our page about the COUNTIF function to learn all you need to know about this powerful function.
This tutorial demonstrates how to use the SEARCH Function in Excel and Google Sheets to locate the position of text within a cell.
What Is the SEARCH Function?
The Excel SEARCH Function “searches” for a string of text within another string. If the text is found, SEARCH returns the numerical position of the string.
Note: SEARCH is NOT case-sensitive. This means “text” will match “TEXT”. To search text with case-sensitivity use the FIND Function instead.
How to Use the SEARCH Function
The Excel SEARCH Function works in the following way.
=SEARCH("e", B3)
Here, Excel will return 1, since “e” is the first character in “elephant”.
Below are a few more examples.
Start Number (start_num)
Optionally, you can define a Start Number (start_num). start_num tells the SEARCH function where to start the search. If you leave it blank, the search will start at the first character.
=SEARCH(B3,C3)
Now, let’s set start_num to 2, which means SEARCH will start looking from the second character.
=SEARCH(B3,C3,D3)
In this case, SEARCH returns 3: the position of the second “e”.
Important: start_num has no impact on the return value, SEARCH will always start counting with the first character.
Start Number (start_num) Errors
If you do use a start number, make sure it’s a whole, positive number that’s smaller than the length of the string you want to search, otherwise you’ll get an error. You’ll also get an error if you pass a blank cell as your start number:
=SEARCH(B3,C3,D3)
Unsuccessful Searches Return a #VALUE! Error
If SEARCH can’t find the search value, Excel will return a #VALUE! error.
Case-Insensitive Search
The example below demonstrates that the SEARCH function is case-insensitive. We searched for “abc”, but SEARCH returned 1, because it matched “ABC”.
Wildcard Search
You can use wildcards with SEARCH, which enable you to search for unspecified characters.
A question mark in your search text means “any character”. So “?000” in the example below means “Search for any character followed by three zeroes.”
An asterisk means “any number of unknown characters”. Here we’re searching for “1*C”, and SEARCH returns 2 because it matches that with “1-ABC”.
In the next example, we’re searching for “000?” – that is, “000” followed by any character. We have “000”, but it’s at the end of the string, and therefore isn’t followed by any characters, so we get an error
However, if we used an asterisk instead of a question mark – so “000*” instead of “000?”, we get a match. This is because the asterisk means “any number of characters” – including no characters.
How to Split First and Last Names from a Cell with SEARCH
If you have first and last names in the same cell, and you want to give them a cell each, you can use SEARCH for that – but you’ll need to use a few other functions too.
Getting the First Name
The LEFT Function returns a certain number of characters from a string, starting from the left.
If we use SEARCH to return the position of the space between the first and last name, subtract 1 from that, we know how long the first name is. Then we can just pass that to LEFT.
The first name formula is:
=LEFT(B3,SEARCH(“ “,B3)-1)
Getting the Last Name
The RIGHT Function returns a certain number of characters from the right of a string.
To get a number of characters equal to the length of the last name, we use SEARCH to tell us the position of the space, then subtract that number from the overall length of the string – which we can get with LEN.
The last name formula is:
=RIGHT(B3,LEN(B3)-SEARCH(“ “,B3))
Note that if your name data contains middle names, the middle name will be split into the “Last Name” cell.
Using SEARCH to Return the nth Character in a String
As noted above, SEARCH returns the position of the first match it finds. But by combining it with CHAR and SUBSTITUTE, we can use it to locate later occurrences of a character, such as the second or third instance.
Here’s the formula:
=SEARCH(CHAR(134),SUBSTITUTE(D3,C3,CHAR(134),B3))
It might look a little complicated at first, so let’s break it down:
- We’re using SEARCH, and the string we’re searching for is “CHAR(134)”. CHAR returns a character based on its ASCII code. CHAR(134) is a dagger symbol – you can use anything here as long it doesn’t appear in your actual string.
- SUBSTITUTE goes through a string and replaces one character or substring for another. Here, we’re substituting the string we want to find (which is in C3) with CHAR(134). The reason this works, is that SUBSTITUTE’s fourth parameter is the instance number, which we’ve stored in B3.
- So, SUBSTITUTE swaps the nth character in the string for the dagger symbol, and then SEARCH returns the position of it.
Here’s what it looks like:
Finding the Middle Section of a String
Imagine you have many serial numbers with the following format:
AB1XCDC-1BB/BB99
You’ve been asked to pull out the middle section of each one. Rather than doing this by hand, you can combine SEARCH with MID to automate this task.
The MID Function returns a portion of a string. Its inputs are a text string, a start point, and a number of characters.
Since the start point we want is the character after the hyphen, we can use SEARCH to get the hyphen’s position, and add 1 to it. If the serial number was in B3 we’d use:
=SEARCH("-",B3)+1
To get the number of characters we want to pull out from here, we can use search to get the position of the forward slash, subtract the position of the hyphen, and then subtract 1 so ensure we don’t return the forward slash itself:
=SEARCH("/",B3)-SEARCH("-",B3)-1
Then we simply plug these two formulas into MID:
SEARCH Vs FIND
SEARCH and FIND are similar functions. They both return the position of a given character or substring within another string. However, there are two key differences:
- FIND is case sensitive but SEARCH is not
- FIND does not allow wildcards, but SEARCH does
You can see a few examples of these differences below:
SEARCH in Google Sheets
The SEARCH Function works exactly the same in Google Sheets as in Excel:
Additional Notes
The SEARCH Function is a non case-sensitive version of the FIND Function. SEARCH also supports wildcards. Find does not.
SEARCH Examples in VBA
You can also use the SEARCH function in VBA. Type:
application.worksheetfunction.search(find_text,within_text,start_num)
For the function arguments (find_text, etc.), you can either enter them directly into the function, or define variables to use instead.
Return to the List of all Functions in Excel