Velocity - Splitting a string into an array | Community
Skip to main content
New Participant
October 18, 2017
Solved

Velocity - Splitting a string into an array

  • October 18, 2017
  • 1 reply
  • 21561 views

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!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

split() will work, sure.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
October 18, 2017

split() will work, sure.

New Participant
October 19, 2017

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
New Participant
October 19, 2017

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.