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
Super User

@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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,694)