Print Page | Close Window

Formula to Reverse String

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=22814
Printed Date: 04 May 2024 at 7:34am


Topic: Formula to Reverse String
Posted By: NCGOV
Subject: Formula to Reverse String
Date 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



Replies:
Posted By: kevlray
Date 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




Posted By: NCGOV
Date 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


Posted By: kevlray
Date 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.



Print Page | Close Window