How do I get rid of trailing and embedded spaces in a string?

Viewed 13668

I am writing a program that converts national and international account numbers into IBAN numbers. To start, I need to form a string: Bank ID + Branch ID + Account Number + ISO Country Code without the trailing spaces that may be present in these fields. But not every account number has the same length, some account numbers have branch identifiers while others don't, so I will always end up with trailing spaces from these fields.

My working storage looks something like this:

      01 Input-IBAN.
          05 BANK-ID                    PIC N(10) VALUE "LOYD".
          05 BRANCH-ID                  PIC N(10) VALUE "     ".
          05 ACCOUNT-NR                 PIC N(28) VALUE "012345678912   ". 
          05 COUNTRY-CODE               PIC N(02) VALUE "GB".
      01 Output-IBAN                    PIC N(34).

I've put some values in there for the example; in reality it would depend on the input. The branch code is optional, hence me leaving it empty in the example.

I basically want to go from this input strung together: "LOYD 012345678912 GB"

to this: "LOYD012345678912GB"

Does anyone know a way to do this that does not result in performance issues? I have thought of using the FUNCTION REVERSE and then using an INSPECT for tallying leading spaces. But I've heard that's a slow way to do it. Does anyone have any ideas? And maybe an example on how to use said idea?

EDIT: I've been informed that the elementary fields may contain embedded spaces.

2 Answers
Related