Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: Formula to Reverse String Post Reply Post New Topic
Author Message
NCGOV
Newbie
Newbie
Avatar

Joined: 04 Oct 2016
Online Status: Offline
Posts: 24
Quote NCGOV Replybullet Topic: Formula to Reverse String
    Posted: 22 Aug 2019 at 3:27am
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
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 22 Aug 2019 at 4:35am
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


IP IP Logged
NCGOV
Newbie
Newbie
Avatar

Joined: 04 Oct 2016
Online Status: Offline
Posts: 24
Quote NCGOV Replybullet Posted: 22 Aug 2019 at 5:40am
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
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 23 Aug 2019 at 4:54am
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.
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.031 seconds.