Skip to content

How to find the nth occurrence of a character in Excel in Excel

Locate the second, third or nth hyphen, comma or space in text with SUBSTITUTE and FIND, or cut at the nth delimiter with TEXTAFTER.

A code such as EU-DE-BER-0042 has three hyphens, and you need what comes after the second one, or the position of the third. FIND returns the position of the first match. To reach the nth, you have to get past the earlier ones.

For the text after or before the nth delimiter, the shortest route in current Excel is TEXTAFTER or TEXTBEFORE, which have an instance argument:

=TEXTAFTER(A2, "-", 2)

That returns everything after the second hyphen. With -1 as the instance it counts from the end.

For the position of the nth character, use a trick with SUBSTITUTE. Its fourth argument says which occurrence to replace. Replace only the third hyphen with a character that does not occur in your text, and FIND that character:

=FIND("~", SUBSTITUTE(A2, "-", "~", 3))

SUBSTITUTE swaps only the third hyphen for a tilde. FIND then returns the position of the tilde, which is the position of the third hyphen in the original text. Choose a replacement character that never appears in the data; a tilde is usually safe.

To cut out the text between the second and third hyphen with the position known, wrap the pieces in MID. This works in every version.

If the nth occurrence does not exist, FIND returns #VALUE! and TEXTAFTER returns #N/A. Wrap the formula in IFERROR if some rows have fewer delimiters. The Logic and text track practises splitting text with these functions.

2 ways to do it

1TEXTAFTER or TEXTBEFORE with an instance number

=TEXTAFTER(A2,"-",2)

  1. Type =TEXTAFTER(A2,"-",2) for the text after the second hyphen.
  2. Use TEXTBEFORE the same way for the text before the nth delimiter.
  3. Use a negative instance number to count from the end of the text.

2SUBSTITUTE and FIND for the position

=FIND("~",SUBSTITUTE(A2,"-","~",3))

  1. Replace only the nth hyphen with a character that does not appear in the text, such as ~.
  2. FIND the position of that character. It is the position of the nth hyphen in the original.

Try it yourself

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In B2, return the character position of the third hyphen in A2. Fill down to B7.

Answer in B2, then it fills down to B7.

B2
A
B
C
D
1
Code
Position of 3rd hyphen
After 2nd hyphen
2
EU-DE-BER-0042
3
NA-US-NYC-0107
4
AP-JP-TYO-0023
5
EU-FR-PAR-0318
6
NA-CA-TOR-0066
7
AP-SG-SIN-0151

Learn it properly, one graded sheet at a time.

Logic and text covers this in lesson 9, SUBSTITUTE.