SOLVED

Use velocity to reverse string field

Go to solution
trosen-cf
Level 1

Hi All,

 

Is there a way to use a script to reverse a 5 character string field on the Person record?

 

Example:

Pre-Script: ABCDE

Post-Script: EDCBA

 

I've been trying to adjust this script, but cannot identify which key to use.

 

Best,

Thomas

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator
#set( $original = "ABCDE" )
#set( $reversed = "" )
#set( $codeUnits = $original.split("") )
#foreach( $i in [$math.sub($codeUnits.size(),1)..0] )
#set( $reversed = $reversed + $codeUnits[$i] )
#end

View solution in original post

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator
#set( $original = "ABCDE" )
#set( $reversed = "" )
#set( $codeUnits = $original.split("") )
#foreach( $i in [$math.sub($codeUnits.size(),1)..0] )
#set( $reversed = $reversed + $codeUnits[$i] )
#end
dillonlee02
Level 3

This is great information. I have not used Velocity script before, but am commenting so I can follow this topic for learning purposes in my growth and development! Thanks for sharing.

trosen-cf
Level 1

Thank you @SanfordWhiteman. This works beautifully. 

SanfordWhiteman
Level 10 - Community Moderator

Can I ask what the use case is for this? Seems pretty rare to get strings in reverse!

 

Also, must’ve saved my last post too quickly: thought I’d added the disclaimer that this doesn’t work with surrogate pairs nor with compound graphemes (combining marks, emoji complexions, etc.). In other words, as the variable name suggests, it works on 16-bit code units.