Velocity - Splitting a string into an array | Community
Skip to main content
Level 9
Oct 18, 2017 10:16 PM UTC
Solved

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!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Oct 18, 2017 10:33 PM UTC

split() will work, sure.

8 replies

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Oct 18, 2017 10:33 PM UTC

split() will work, sure.

Translate
Level 9
Oct 19, 2017 9:12 PM UTC

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?

Translate
SanfordWhiteman
Level 10
Oct 19, 2017 9:24 PM UTC

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.

Translate