web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Expression for finding...
Power Automate
Unanswered

Expression for finding Nth character within a string

(0) ShareShare
ReportReport
Posted on by 16

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?

Categories:
I have the same question (0)
  • crowdjs Profile Picture
    133 on at

    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.

  • KJ_Cheetham Profile Picture
    16 on at

    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.

  • Verified answer
    KJ_Cheetham Profile Picture
    16 on at

    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.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard