Xth Occurrence of a string within another (1 Viewer)

deletedT

Guest
Local time
Today, 05:30
Joined
Feb 2, 2019
Messages
1,218
nth Occurrence of a string within another

Is there any function that can find the xth occurrence of a string within another?

InStr finds the first. How about if I want to find the position of third "K" in this string: "HEJKK98OBK12K7"?

Note:I have written a function that can do it. I'm looking for a function like InStr in VBA library.

Thank you.
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:30
Joined
Oct 29, 2018
Messages
21,358
Sorry. I am not aware of any built-in function that does that.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:30
Joined
May 21, 2018
Messages
8,463
No.
Extra text.
 

deletedT

Guest
Local time
Today, 05:30
Joined
Feb 2, 2019
Messages
1,218
How about showing the readers your function, just in case someone else needs same?

I'm home and away from my desk. But my function was something like this:
Code:
Function nthPos(str As String, SearchForThis As String, Nth As Integer) As Integer
    
    ' Returns 0 if not found
    Dim Counter as integer, pos as integer
    nthPos = 0
    pos = 0

    For Counter = 1 To Nth
        pos = InStr(pos + 1, str, SearchForThis)
        If pos = 0 Then Exit Function
    Next
    
    nthPos = pos
    
End Function
 
Last edited:

Users who are viewing this thread

Top Bottom