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
Hey @smozgur Please critique posting style before I get too out of line. Thanks!!
 
@smozgur, also, should a subsequent LAMBDA that uses KthCharInstance be its own post? I'm thinking that I might just post it as a comment here? Don't want to start off on the wrong foot.
 
Hey @smozgur Please critique posting style before I get too out of line. Thanks!!
Thanks, @tboulden. It looks perfect!
I only changed the title to be all uppercase, as an Excel function, and also reduced the indent in the Lambda function code.

@smozgur, also, should a subsequent LAMBDA that uses KthCharInstance be its own post? I'm thinking that I might just post it as a comment here? Don't want to start off on the wrong foot.
Yes. Please remember to refer to this thread in the new function post.
You can see how I did in my SLUGIFY.PLUS function:
 
Yes. Please remember to refer to this thread in the new function post.
Let me clarify. This function does something on its own. So, it is the right way to keep this function alone.
If your new function is going to use this function, then you can refer to this function.

Personally, I would embed the logic that exists in this function into the new function, and keep the new function independent (where it is possible), because the Lambda function definition kind of promises for that. However, it is not something practical all the time for complex formulas, or it might be also a better idea to reuse the functions that could be used in many other new functions.
So, it actually depends on your approach.
 
Let me clarify. This function does something on its own. So, it is the right way to keep this function alone.
If your new function is going to use this function, then you can refer to this function.

Personally, I would embed the logic that exists in this function into the new function, and keep the new function independent (where it is possible), because the Lambda function definition kind of promises for that. However, it is not something practical all the time for complex formulas, or it might be also a better idea to reuse the functions that could be used in many other new functions.
So, it actually depends on your approach.
Just as a quick example before I make a separate thread, my LAMBDA ALLCHARPOSITIONS just finds the total number of chars N in a string, then passes a SEQUENCE(N) to the k parameter of KTHCHARINSTANCE. So it doesn't do anything new; I think of it as a corollary to KTHCHARINSTANCE almost.
 
just finds the total number of chars N in a string
This makes more sense as "finds the total number of myChar in myStr = N, then passes SEQUENCE(N) to the k parameter...
 
Just as a quick example before I make a separate thread, my LAMBDA ALLCHARPOSITIONS just finds the total number of chars N in a string, then passes a SEQUENCE(N) to the k parameter of KTHCHARINSTANCE. So it doesn't do anything new; I think of it as a corollary to KTHCHARINSTANCE almost.
If you post here, then it will be a further discussion as an extension to KTHCHARINSTANCE function. If you post a new thread, then it will be a new function.
If you ask me, then I would say they are different functions.
Also, consider this, we have a voting system in the Lambda forum, and the posted functions can be voted by the other members. If you don't create a new thread then it won't be voted as a standalone function.

Please refer to my SLUGIFY and SLUGIFY.PLUS functions once again. They are doing the same thing, but I improved the original function in the PLUS version. To me, the PLUS version is a brand new version, like Office 2019 vs Office 365. In fact, in your sample above, they are completely different functions doing different things.

Finally, it really depends on how you'd like to proceed. There is no harm to post a new thread and it is my suggestion in this case.
 
Thanks @smozgur, I'll post a different thread; I think I have a concise way of including the logic for KTHCHARINSTANCE and will also link back here.
 

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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