How would you split up a text string into 3 values that can then be used in a SC?

Hey all,

I have an address line, e.g. San Francisco, California, United States.

I’d like to split the string up so each split is useable as a variable in a Server Action.

I’ve tried split string in a set value, setting it as text and as an array but don’t seem to be returning anything more than the same string in output in dev tools.

Screen Shot 2020-09-10 at 5.39.30 pm

I’ve checked this post out thinking it might solve my need, but couldn’t find a solution here:

I’ve also tried doing it on the front end - with ‘split’ in the text formatter, but I’m missing the part (which is no doubt obvious) that would enable me to return the three items from the string.

Will keep playing around but appreciate anyone pointing me in the right direction.

Split creates an array, so you should be able to get at the individual items using the index for each.

For example, the first item is:

$_POST.ac_input.split(',')[0]

Hey Ken!

I was looking at the output and it only returned a single result, instead of [0] San Francisco [1] California [2] United States which would be as expected - if the split / array worked correctly this would display as such in dev tools?

Sorry, not understanding your last comment.

As the output in dev tools, I would expect it to display:

[0] San Francisco
[1] California
[2] United States

If it did then your solution would work perfectly.

But the output in dev tools is just displaying: [0] San Francisco, California, United States

Which leads me to believe that it’s not splitting the text?

You used exactly what I provided?

Got it! It didn’t like the space, I was trying to split with the ', ’ instead of just ‘,’ like you wrote.

I’ll do another formatter to get rid of the space and should be good to go. Thank you!!

1 Like

Sorry, I forgot to call out that change I made when adding the index.

1 Like