Formula to Reverse String

Printed From: Crystal Reports Book — Forum Name: Technical Questions

I am looking to create a formula that will reverse a string.

String - LocationA/LocationB

I want to reverse the text to display LocationB/LocationA

There is always a "/" between the two locations


Answer
local stringvar a_st := "";
local stringvar b_st := "";
local stringvar c_st := "";

if instr({table.street}, "/") > 0
    then
        (
            a_st := split({table.street}, "/")[2];
            b_st := split({table.street}, "/")[1];
            if a_st < b_st
                then c_st := a_st & "/" & b_st
            else c_st := b_st & "/" & a_st;
        )
else c_st := {table.street};

c_st


Edited by NCGOV - 11 Mar 2020 at 3:04am
So with using variables and the INSTR function, you should be able to 'grab' each substring and reverse the order.

so something like this.

stringvar firstString;
stringvar secString;

firstString := left({field}, instr({field}, '/') -1 );
secString := right({field}, instr({field}, '/') -1); //Could used MID

secString+'/'+firststring


Thank you so much for taking the time to reply!

The formula returned an error "String length is less than 0 or not an integer"

stringvar firstString;
stringvar secString;

firstString := left({street}, instr({street}, '/') -1 );
secString := right({street}, instr({street}, '/') -1);

secString+'/'+firststring
Apparently some of your data has null values.  In that case you will need to check for that (if statement?) and figure out what you wan to do with empty strings.