10
Views
5
Comments
Solved
Client Side String Split on first delimiter
Application Type
Reactive

Hello,

I have a string "URL=/MyPage?ReportId=10&isEdit=True" that I want to store in a cookie where the cookie name is "URL" and the value is "/MyPage?ReportId=10&isEdit=True". At this time I'm trying to split the string using "=" delimiter but it breaks the value of my cookie. I need a function that splits just on the first delimiter.

I would like to have this done in the client side insted of calling the string split server action.


Thanks in advanced,
João Varela

Rank: #94
Solution

Hello João,

Hope you are doing well.

I believe that you want to achieve something like this:


Basically I'm using the following JavaScript code:

var str = $parameters.Text.split($parameters.Delimiter);
$parameters.FirstValue = str.shift();
$parameters.SecondValue = str.join($parameters.Delimiter);

where the $parameters.Delimiter is "=" and $parameters.Text is your string.


So, I'm using split method to do the splits, resulting in an array:

["URL", "/MyPage?ReportId", "10&isEdit", "True"]

 

Then the shift method is going to remove the first element of the array and return that same element. So the return of this will be your first value: "URL" -> Cookie Name

Then you just need to use join method to concatenate back your remaining values of the array and separate them by the given delimiter ("=") resulting in your second value: "/MyPage?ReportId=10&isEdit=True" -> Cookie Value


Please refer to attached OML file with this solution :)
I created a generic client action called StringSplit_TwoValues and a Screen1 to do some tests.


Kind regards,

Rui Barradas

StringSplitTwoValues.oml

Rank: #6307

Thank you Rui. It's exactly the solution that I was searching for.

Rank: #94

That's nice to hear that!

You're most welcome João :)


Kind regards,

Rui Barradas

Rank: #170

Hi João,

What if you use Regex?

For example I found a how to in forge and tried to use see here:


using the string you passed with a pattern   

=.*

I was able to return just what comes after the equal sign.


Also,

If you want the text without including the first equal you can use this pattern:

(?<=\=).*


This is the component that shows you how to do it...

It is very simple and can be done from both, Client and Server side.


Hope it helps,

RR :)

Rank: #6307

Thanks for your reply Raphael but I was trying to stay away from regex.

Rank: #94
Solution

Hello João,

Hope you are doing well.

I believe that you want to achieve something like this:


Basically I'm using the following JavaScript code:

var str = $parameters.Text.split($parameters.Delimiter);
$parameters.FirstValue = str.shift();
$parameters.SecondValue = str.join($parameters.Delimiter);

where the $parameters.Delimiter is "=" and $parameters.Text is your string.


So, I'm using split method to do the splits, resulting in an array:

["URL", "/MyPage?ReportId", "10&isEdit", "True"]

 

Then the shift method is going to remove the first element of the array and return that same element. So the return of this will be your first value: "URL" -> Cookie Name

Then you just need to use join method to concatenate back your remaining values of the array and separate them by the given delimiter ("=") resulting in your second value: "/MyPage?ReportId=10&isEdit=True" -> Cookie Value


Please refer to attached OML file with this solution :)
I created a generic client action called StringSplit_TwoValues and a Screen1 to do some tests.


Kind regards,

Rui Barradas

StringSplitTwoValues.oml

Rank: #6307

Thank you Rui. It's exactly the solution that I was searching for.

Rank: #94

That's nice to hear that!

You're most welcome João :)


Kind regards,

Rui Barradas