KTHCHARINSTANCE

=KTHCHARINSTANCE(myStr, myChar, k)

myStr
any text string
myChar
character to search for
k
the ordinal occurrence of interest (i.e. KTHCHARINSTANCE("test","t",2) = 4 for the second "t")

KTHCHARINSTANCE finds the nth occurrence of a character.

tboulden

Board Regular
Joined
Jan 14, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
KTHCHARINSTANCE is just a LAMBDA-packaged version of the formula here: Finding the Nth Occurrence of a Character (Microsoft Excel)

This is a helper function for a more substantial LAMBDA I'd like to post, but I want to get used to how post creation editing works, so I'll do a couple small ones first.

Takes advantage of SUBSTITUTE's 4th parameter instance_num and a unique character CHAR(1) to find the position of instance_num in the string. The IFERROR wrap could probably be better, but was a quick stopgap for the logic of my later formula.

Excel Formula:
=LAMBDA(
    myStr,
    myChar,
    k,
    IFERROR(
        FIND(
            CHAR(1),
            SUBSTITUTE(myStr,myChar,CHAR(1),k)
        ),
        LEN(myStr)+1
    )
)
LAMBDA_UnpackLet.xlsx
ABCD
1myStrmyCharkKTHCHARINSTANCE
2This is a test.s14
3This is a test.s27
4This is a test.t111
5This is a test.t316
Sheet1
Cell Formulas
RangeFormula
D2:D5D2=KTHCHARINSTANCE(A2,B2,C2)
 
Last edited by a moderator:
Upvote 0
I have two questions about the function.
  1. Although it is not a big deal, perhaps NTHCHARINSTANCE could be used as the function name?
  2. Shouldn't the function return NA() if an error such as char not found, invalid parameter, etc? I do understand this function will be used in another function, however, I believe it should be handled in the other function instead. The function should better provide a useful result including its error handling.
Also, the myChar argument is defined as the character to search for. However, the function also allows searching for a word in the string, which is great.
 
All good points, I've re-worked the formula with these things in mind, and also to consolidate the AllCharPositions that utilized this LAMBDA. Please see below; I propose something like the following:
  • STARTPOS(txt,str,n) =>
    • STARTPOS("This is This","is",1) = 2
    • STARTPOS("This is This","is",4) = #N/A
    • If we treat param n as optional: STARTPOS("This is This","is",) = array(3,6,11)
    • If we treat param n as required: STARTPOS("This is This","is","ALL") = array(3,6,11)
Optional n param version:
Excel Formula:
=LAMBDA(
    txt,
    str,
    n,
    LET(
        k,
        IF(
            ISBLANK(n),
            SEQUENCE(
                LEN(SUBSTITUTE(txt,str,CHAR(1)))
                -
                LEN(SUBSTITUTE(txt,str,""))
            ),
            n
        ),
        IFERROR(
            FIND(
                CHAR(1),
                SUBSTITUTE(txt,str,CHAR(1),k)
            ),
            NA()
        )
    )
)

Required n param version:
Excel Formula:
=LAMBDA(
    txt,
    str,
    n,
    LET(
        k,
        IF(
            n = "ALL",
            SEQUENCE(
                LEN(SUBSTITUTE(txt,str,CHAR(1)))
                -
                LEN(SUBSTITUTE(txt,str,""))
            ),
            n
        ),
        IFERROR(
            FIND(
                CHAR(1),
                SUBSTITUTE(txt,str,CHAR(1),k)
            ),
            NA()
        )
    )
)
 
Last edited:

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top