Use velocity to reverse string field | Community
Skip to main content
Level 1
February 22, 2022
Solved

Use velocity to reverse string field

  • February 22, 2022
  • 1 reply
  • 2628 views

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

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
#set( $original = "ABCDE" ) #set( $reversed = "" ) #set( $codeUnits = $original.split("") ) #foreach( $i in [$math.sub($codeUnits.size(),1)..0] ) #set( $reversed = $reversed + $codeUnits[$i] ) #end

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
February 23, 2022
#set( $original = "ABCDE" ) #set( $reversed = "" ) #set( $codeUnits = $original.split("") ) #foreach( $i in [$math.sub($codeUnits.size(),1)..0] ) #set( $reversed = $reversed + $codeUnits[$i] ) #end
trosen-cfAuthor
Level 1
February 23, 2022

Thank you @sanfordwhiteman. This works beautifully. 

SanfordWhiteman
Level 10
February 23, 2022

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.