cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
w1sd0m
Advocate II
Advocate II

Substitute Whole Word using in or exactin

Hello,

I am building a find and replace tool for a canvas app. The text is HTML.
When I use Substitute using in or exactin, it subs partial match in addition to the whole word I am replacing.
For example:

'string' will sub all occurrences of 'string' in the HTMLtext, including the word 'strings'

The result is that any formatting used as the sub will be applied to the partial match. I am thinking of using a len() for Boolean comparison but would like to ask for recommendations.

 

Actual code:

If(
    Substitute(First( Split( Last( Split( richtextFind.HtmlText, "<p>" ) ).Result, "</p>" ) ).Result,"&nbsp","&#160")
    in
    varE,
    Set(varE,
        Substitute(varE,
            Substitute(First( Split( Last( Split( richtextFind.HtmlText, "<p>" ) ).Result, "</p>" ) ).Result,"&nbsp","&#160"),
            First(Split(Last(Split(richtextReplace.HtmlText,"<p>")).Result,"</p>")).Result)
    )
)
7 REPLIES 7
w1sd0m
Advocate II
Advocate II

@RandyHayes  any low hanging fruit here? 🙂

RandyHayes
Super User
Super User

@w1sd0m 

You did hit my radar with this one!  I needed to pull up some apps that I have where I've done this properly.

The problem in your formula is that you are trying to do this in a "program like" way.  The substitute function is not decerning, so it will just replace any occurrence that it sees.

What you really need to do is find the place that the occurrence starts and, in your case, base that on a word boundary.  Then rebuild the string - left up to the occurrence, then the replacement, then right from the occurrence plus length to the end.

 

The above is the basics.  I found where I use this concept in the past, and I have it as a component that does multiple find and replace in text.  So, it is a little more complex for what you appear to be doing - which is to just find and replace a single text.

 

I am not following your requirements based on your formula completely, it almost appears that you are just trying to substitute &nbsp; with &#160 so I am not sure where you are referencing "string" as your post describes.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Yea the sub for &nbsp with &#160 resolved the finding the string from the RichTextEditor that contains spaces in the variable text string. I can share a clip of how this is currently working.

RandyHayes
Super User
Super User

@w1sd0m 

So what is your ultimate goal on the formula?  Are you trying to substitute just one or multiple words in the html text?

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
w1sd0m
Advocate II
Advocate II

@RandyHayes 
I am thinking of the same approach. Will update when I solve.

Goal is to sub one word or phrase at a time, formatting inclusive. I saw the cannon you built back in December for the multi-word replacement. Very nice BTW! I'm okay with making the small changes via a feature like this. Manually editing the Rich Text was taking too long. 

 

Since I am trying to be rigid on what I am finding, the sub &nbsp with &#160 catches blank spaces unintentionally formatted with the word preceding it. Sometimes the bold and italic tags will wrap the space as well.. see

w1sd0m_0-1645734056162.png

 



 

RandyHayes
Super User
Super User

@w1sd0m 

Yes, the multi-word replacement back in December is a good reference.  I eventually took that concept and put it into a component that I reuse for find/replace and also for highlighting words.

 

I would start with the post from Dec and see where that takes you.  

Just let me know if you get stuck!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (3,115)