Search code examples
crystal-reports

Crystal Report: Split by delimiter and reorder a string


I don't have access to directly amend the stored procedures that are used in our Crystal Reports, they're created by the company who's software we're using. In order to get around paying for the amend, I was wondering if there was the means to do what I need in a formula.

I'm OK with basic stuff CR formulae, but I can't work this out.

We receive a field of material composition for textiles that is generated as below. What I would like to do is to separate the different materials and reverse the order, so that the highest percentage is at the beginning of the string. Is anyone able to help? Or maybe the first question should be is this possible?

Thanks in advance!

Current Output:

  1. 15% Other (RM0262), 15% Polylactic acid (PLA) (RM0198), 85% Cotton (RM0104)
  2. 13.22% Polylactic acid (PLA) (RM0198), 13.52% Other (RM0262), 86.48% Cotton (RM0104)
  3. 17.75% Other (RM0262), 82.25% Cotton (RM0104)
  4. 100% Cotton (RM0104)

Desired Output:

  1. 85% Cotton (RM0104), 15% Polylactic acid (PLA) (RM0198), 15% Other (RM0262)
  2. 86.48% Cotton (RM0104), 13.52% Other (RM0262), 13.22% Polylactic acid (PLA) (RM0198)
  3. 82.25% Cotton (RM0104), 17.75% Other (RM0262)
  4. 100% Cotton (RM0104)

Solution

  • local stringvar input := "15% Other (RM0262), 15% Polylactic acid (PLA) (RM0198), 85% Cotton (RM0104)";
    local stringvar output := "";
    local stringvar delimiter := "";
    local numbervar i;
    
    local stringvar array originalArray := Split(input, ", ");  
    For i := Ubound(originalArray) to 1 step -1 do
    (
    output := output + delimiter + originalArray[i];
    delimiter := ", ";
    );
    
    output;