SOLVED

Re: Velocity - Splitting a string into an array

Go to solution
Phillip_Wild
Level 10 - Community Advisor

Velocity - Splitting a string into an array

Hey everyone

How would I take a field with values separated by backslashes "/" and split it into an array that I specify? I don't think .split will do the job - something like parseStringDelimiter or $convert.toString , or perhaps a combination!

Any ideas? My data looks like this:

ABCD/GIST/SPIT

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Splitting a string into an array

split() will work, sure.

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Splitting a string into an array

split() will work, sure.

View solution in original post

Phillip_Wild
Level 10 - Community Advisor

Re: Velocity - Splitting a string into an array

Thanks Sanford.

I'm playing around with it and unfortunately having some trouble. I understand I need to escape the special character "/" in order to split the string based on that - but I can't seem to even get a basic split working.

Here's my code:

#set ($DossierArray = ${lead.wishListtoWin2016Dossiers}.split('\/'))

$DossierArray

And I get this error:

Cannot get email content-

An error occurred when procesing the email Body!

Lexical error, Encountered: "s" (115), after : "." at *unset*[line 12, column 66] near

    </div>  
  
${mktmail.Optout}
</body>
</html>

That's a similar error to the one I've often got when I haven't removed whitespace  - but I ran it through an online whitespace removal tool, so I don't think it's that. But my email that I'm previewing is ONLY the email script token, so it must be related to that token.

Strangely, I can't even seem to get a simple string split to work either. Using this code:

#set ($test = "This string contains the letter s")

$test.split('s')

I get this error:

[Ljava.lang.String;@151e693a

Clearly something is going horribly wrong here Any ideas why?

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Splitting a string into an array

VTL doesn't use basic backslash escapes, only explicit Unicode: "\u002f" is a forward slash.

The second case isn't an error, it's showing you successfully split into an array of strings. You'd then have to loop over it or seek an array index.

Phillip_Wild
Level 10 - Community Advisor

Re: Velocity - Splitting a string into an array

Thanks Sanford. Unfortunately I'm still having trouble - and I don't think it's related explicitly to that .split functionality now. When I use:

#set ($DossierArray = ${lead.wishListtoWin2016Dossiers}.split("\u002f"))

$DossierArray

I'm still getting that same lexical error above. Even when I replace the Unicode info with "a", it still gives me the same error. What does "s (115) mean?

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Splitting a string into an array

Take out the curly braces.

Nicholas_Manojl
Level 9

Re: Velocity - Splitting a string into an array

#set ($string = "ABCD/GIST/SPIT")

#set ($output = $string.split('/'))

$output[0]

$output[1]

$output[2]

Works for me

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Splitting a string into an array

Also, Nicho is right that the forward slash doesn't actually need to be escaped in Velocity/Java regex.

It's still not a bad idea to escape it (as "\u002f" above ) so you don't get confused in JavaScript regex literals, where it is reserved (in JS, "a/b/c/d".split(///) is a syntax error and has to be "a/b/c/d".split(/\//) or "a/b/c/d".split(/\u002f/) ).

Phillip_Wild
Level 10 - Community Advisor

Re: Velocity - Splitting a string into an array

Thanks everyone, it seems to be working correctly now!

Once I removed the brackets and referenced a specific part of the array it is working fine!