cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
csommerfield
Helper I
Helper I

Remove last character in string IF the last character is a specific character like ; or |

In my form I have a couple of text inputs that add to a collection. On input, it separates each item in the collection with |.

 

It then saves the collection to a SharePoint field and it will look something like

 

1;test1;no;yes|2;test2;no;yes|3;test3;yes;yes|4;test4;no;no|

 

I'm displaying the collection in a gallery by splitting it at |. The problem is on the last | it assumes there's another item after it, but there isn't, that's support to be the end.

 

I used Left(varStringofCollection, len(varStringofCollection) -1) to remove the ending | and that works.

 

But the problem I noticed is that some users will open the item multiple times before saving it and instead of it opening as 

1;test1;no;yes|2;test2;no;yes|3;test3;yes;yes|4;test4;no;no

 

It will open as
1;test1;no;yes|2;test2;no;yes|3;test3;yes;yes|4;tes

 

basically removing parts of the collection. I want to only remove the last | from the string. If it's not there, then do nothing.

 

Any ideas?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

FYI I got it to work but only by removing the with. Thanks for the help

 

Set(
        varMonitorPressureAllItems,
        If(
            Right(
                varMonitorPressureAllItems,
                1
            ) = "|",
            Left(
                varMonitorPressureAllItems,
                Len(varMonitorPressureAllItems) - 1
            ),
            varMonitorPressureAllItems
        )
    );

View solution in original post

6 REPLIES 6
mdevaney
Super User III
Super User III

@csommerfield 
Assuming you have a string value like this:

Set(myText, "1;test1;no;yes|2;test2;no;yes|3;test3;yes;yes|4;test4;no;no|")

 

We can conditionally remove the last item if it is a pipe |  or otherwise do nothing.

Set(
    With(
        {varText: "1;test1;no;yes|2;test2;no;yes|3;test3;yes;yes|4;test4;no;no|"},
        If(Right(varText,1)="|",
        Left(varText, Len(varText-1)),
        varText
    )
)

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Awesome! Thank you for the quick reply.

 

What is the With doing? Have never used that before.

 

Also, the vartext would be pulled from a SP list. So mine would look something like

 

Set(
    With(
        {varMonitorPressureAllItems: ThisItem.MonitorPointPressure},
        If(Right(varMonitorPressureAllItems,1)="|",
        Left(varMonitorPressureAllItems, Len(varMonitorPressureAllItems-1)),
        varMonitorPressureAllItems
    )
)

Getting errors though. Is the With necessary or can I just the If Right? 

@csommerfield 
WITH allows you to store a variable for a specific section of code and then immediately deletes it when the function is complete.  I like to use it because my code will only make 1 call to SharePoint instead of up-to 3.

 

What errors are you getting.  Please supply the full error message and a screenshot of your code. 

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up." 

This is the call that happens on select of the gallery button. If I input yours the whole thing turns red. I may now be putting it in the right place?

Set(
    varWAPItem,
    ThisItem
);
If(
    IsBlank(varWAPItem.MonitorPointPressure),
    Clear(colMeasurePointPressure),
    Set(
        varMonitorPressureAllItems,
        varWAPItem.MonitorPointPressure
    );
    ForAll(
        Split(
            varMonitorPressureAllItems,
            "|"
        ),
        Collect(
            colMeasurePointPressure,
            {
                ppLocationItem: PlainText(
                    Last(
                        FirstN(
                            Split(
                                Result,
                                ";"
                            ).Result,
                            1
                        ).Result
                    ).Result
                ),
                ppPSIItem: PlainText(
                    Last(
                        FirstN(
                            Split(
                                Result,
                                ";"
                            ).Result,
                            2
                        ).Result
                    ).Result
                )
            }
        )
    )
);

I think there's some missing syntax, it doesnt like the whole thing.

If(
    IsBlank(varWAPItem.MonitorPointPressure),
    Clear(colMeasurePointPressure),
    Set(
        varMonitorPressureAllItems,
        varWAPItem.MonitorPointPressure
    );
    Set(
    With(
        {varMonitorPressureAllItems: ThisItem.MonitorPointPressure},
        If(Right(varMonitorPressureAllItems,1)="|",
        Left(varMonitorPressureAllItems, Len(varMonitorPressureAllItems-1)),
        varMonitorPressureAllItems
    )
);
    ForAll(
        Split(
            varMonitorPressureAllItems,
            "|"
        ),

 

 

@csommerfield 
Sorry, but that section of wavy code is a little much for me to parse.  I don't necessarily understand what's going on here.  Simplifying the code/question might help.

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

FYI I got it to work but only by removing the with. Thanks for the help

 

Set(
        varMonitorPressureAllItems,
        If(
            Right(
                varMonitorPressureAllItems,
                1
            ) = "|",
            Left(
                varMonitorPressureAllItems,
                Len(varMonitorPressureAllItems) - 1
            ),
            varMonitorPressureAllItems
        )
    );

View solution in original post

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Carousel April Dunnam Updated 768x460.jpg

Urdu Hindi D365 Bootcamp

Dont miss our very own April Dunnam’s The Developer Guide to the Galaxy! Find out what the Power Platform has to offer for the traditional developer.

Top Solution Authors
Top Kudoed Authors
Users online (2,348)