Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

lastindexOf() or a Find() from the right of a string

Like (0) ShareShare
ReportReport
Posted on 27 Jan 2021 17:21:42 by 3,340

I need to find the index of the last instance of a character in a string.  In the Flow expression builder, there is the lastindexOf() statement.  I need a suitable way to do this in PA.  We have one?

Categories:
  • JenniferK Profile Picture
    167 on 18 Aug 2023 at 16:57:31
    Re: lastindexOf() or a Find() from the right of a string

    I was so happy to find this solution! It's clear and succinct. I did however, find a corner case where it fails. Consider the following:

    mystring= "A charge is levied for any instrument or radioactive source that is found to be unsuitable for calibration. The fee covers inspection and return, and is based on the work done prior to the discovery of the fault."
    
    With(
     {_lookingFor: " "}, 
     Find(_lookingFor & Last(Split(mystring, _lookingFor)).Value, mystring) //returns 38, which is the position of the first occurence of the letter o because Find is looking for "o ".
    )

    My usual approach to finding the last _lookingFor in a string is to reverse the string and find the first position of the _lookingFor and then subtract it from the string length and do Left(mystring, someposition). But PowerApps (at least in canvas) doesn't seem to have an easy way to reverse a string.

     

    Do you have any suggestions to deal with the cornercase I've illustrated?

  • DThompsòn Profile Picture
    5 on 29 Jul 2023 at 18:22:26
    Re: lastindexOf() or a Find() from the right of a string

    This was very useful but please note the last line should read : 

     

     

    Find(_lookingFor & Last(Split(_aString, _lookingFor)).Value, _aString)

     

  • Verified answer
    RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on 27 Jan 2021 at 17:32:33
    Re: lastindexOf() or a Find() from the right of a string

    @martinav 

    There is no particular LastIndexOf in PowerApps.

     

    But consider the following formula:

    With({_aString: "This is, may I say, quite and interesting way to go, or get past, or complete",
     _lookingFor: ","},
     If(EndsWith(_aString, _lookingFor), 
     Len(_aString),
     Find(_lookingFor & Last(Split(_aString, _lookingFor)).Result, _aString)
     )
    )

    This will return 65 - which is the last index of the ","

    If the "," in this case is at the end, it will provide the ending location.

     

    I hope this is helpful for you.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Building Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 78 Most Valuable Professional

#2
MS.Ragavendar Profile Picture

MS.Ragavendar 49

#3
mmbr1606 Profile Picture

mmbr1606 41 Super User 2025 Season 1

Overall leaderboard