Remove leading 0 and removing dash

hugh11

New Member
Hi all,

Output:
01-LC27-05-04 PLT1
03-001-11-01 PLT3

I would like to:
1) Remove the leading 0 (zero)
2) Keep first dash but remove the rest of the dashes

Progress 91D

Can someone help.

tia

hugh
 

medu

Member
Code:
FUNCTION whatEverTrimMethod RETURN CHARACTER (txt AS CHARACTER):
    DEFINE VARIABLE idx AS INTEGER   NO-UNDO.

    ASSIGN 
        txt = LEFT-TRIM(txt, '0 ')
        idx = INDEX(txt, '-')
        txt = SUBSTITUTE('&1&2', SUBSTRING(txt, 1, idx), REPLACE(SUBSTRING(txt, idx, -1), '-', '')) NO-ERROR.
    RETURN txt.
END FUNCTION.

MESSAGE whatEverTrimMethod('03-001-11-01 PLT3') SKIP
    whatEverTrimMethod('01LC270504 PLT1')
    VIEW-AS ALERT-BOX INFO BUTTONS OK.

and yeah... i would like a beer, a long holiday and to see spring coming after this long winter ;)
 
Hello @medu

Is there any specific reason of adding SUBSTITUTE () here because, I think both string can be simply be added by using below:

Code:
txt = SUBSTRING(txt, 1, idx) + REPLACE(SUBSTRING(txt, idx, -1), '-', '')

Along with that i ran both codes in multiple iteration and found SUBSTITUTE as little slower. below is code for the same:

Code:
DEFINE VARIABLE itime AS INT NO-UNDO EXTENT 5 INITIAL {&sequence}.
DEFINE VAR i AS INTEGER NO-UNDO.
DEFINE VAR txt AS CHAR INIT "03-001-11-01 PLT3" NO-UNDO.
DEFINE VARIABLE idx AS INTEGER   NO-UNDO.
DEFINE VARIABLE idx1 AS INTEGER   NO-UNDO.

itime[{&sequence}] = ETIME.     

DO i = 1 TO 1000000:

END.

itime[{&sequence}] = ETIME.     

DO i = 1 TO 1000000:


    ASSIGN 
        txt = LEFT-TRIM(txt, '0 ')
        idx = INDEX(txt, '-')
        txt = SUBSTITUTE('&1&2', SUBSTRING(txt, 1, idx), REPLACE(SUBSTRING(txt, idx, -1), '-', '')) NO-ERROR.
END.

itime[{&sequence}] = ETIME.     


DO i = 1 TO 1000000:

    ASSIGN 
        txt = LEFT-TRIM(txt, '0 ')
        idx = INDEX(txt, '-')
        txt = SUBSTRING(txt, 1, idx) + REPLACE(SUBSTRING(txt, idx, -1), '-', '') NO-ERROR.

END.

itime[{&sequence}] = ETIME.  

MESSAGE      
       itime[2] - itime[1] SKIP
       itime[3] - itime[2] SKIP       
       itime[4] - itime[3] SKIP              
    VIEW-AS ALERT-BOX.

Output for me is:

221
2880
2404

Please suggest.
 

TomBascom

Curmudgeon
I suggest that you open a new thread regarding “why use substitute” rather than dredge up an 8 year old thread and launching a tangent.
 
Top