cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Frequent Visitor

Expression for finding Nth character within a string

How do I find the position of the Nth instance of a particular character in a string?

We've got indexOf() and lastIndexOf() to find the first and last at least.

For finding say just the second could use indexOf to get the first, then substring it, and then indexOf again on the resulting substrings.

 

But how to find the Nth, i.e. an arbitrary number?

1 ACCEPTED SOLUTION

Accepted Solutions

I think I've solved my own problem!

 

KJ_Cheetham_0-1606644296453.png

KJ_Cheetham_1-1606644355274.png

 

The substring(..) is 

substring(outputs('Compose_test'),add(indexOf(outputs('Compose_test'),'-'),1))

and the output is correctly 'eeeeee-fffff-ggggggggg-hhhh'.

 

Just need to change the '3' in the loop to whatever number is required.

 

Disclaimer: doesn't efficiently handle putting something like 100 in the loop, but would return just the final section I think in that case.

 

If someone did want an array of all the positions, I'm sure could do similar with a loop to the end of the string.

 

View solution in original post

3 REPLIES 3
Resolver I
Resolver I

This sounds tough, do you have an example of a string you are searching?

First you may need to break the string into an array. See solution posted by @Anonymous here.

https://powerusers.microsoft.com/t5/Building-Flows/Split-string-into-array-of-characters-empty-delimiter-not/td-p/189153

Then maybe count the number of times you see a character and act on the Nth time you see it.

Well what I've got is strings something like:

aaa-bbbb-cccc-eeeeee-fffff-ggggggggg-hhhh

Where each section is variable length.

 

What I need is to be able to strip off an arbitrary number of sections from the left, e.g. stripping off first 3 to give eeeeee-fffff-ggggggggg-hhhh

 

Stripping off just one I've since found I can use: 

substring(variables('test'),add(indexOf(variables('test'),'-'),1))
Stripping off the second I can use the same again, but on the output of the previous one, and so on. So I could probably make that into some kind of loop using the 'Apply to each' action somehow?

 

What would be nice I guess would have been a built-in index or find function that returns an array of the positions of all the matches, not just first or last.

I think I've solved my own problem!

 

KJ_Cheetham_0-1606644296453.png

KJ_Cheetham_1-1606644355274.png

 

The substring(..) is 

substring(outputs('Compose_test'),add(indexOf(outputs('Compose_test'),'-'),1))

and the output is correctly 'eeeeee-fffff-ggggggggg-hhhh'.

 

Just need to change the '3' in the loop to whatever number is required.

 

Disclaimer: doesn't efficiently handle putting something like 100 in the loop, but would return just the final section I think in that case.

 

If someone did want an array of all the positions, I'm sure could do similar with a loop to the end of the string.

 

View solution in original post

Helpful resources

Announcements
PP Bootcamp Carousel

Global Power Platform Bootcamp

Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.

secondImage

Power Platform Community Conference On Demand

Watch Nick Doelman's session from the 2020 Power Platform Community Conference on demand!

MPA Community Blog

Power Automate Community Blog

Check out the community blog page where you can find valuable learning material from community and product team members!

Users online (4,459)