Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

replace in a string

Former Member
0 Likes
788

Hi.

I need to remove som values in a string if it's longer than 18 char. The logic is as follows. If len > 18 start removing '-' from back of the string until (if possible) the length is 18.

I have probelms reaplcing from behind, any ideas?

/jon

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
744

Hi Jon,

Consider these code.

<b>If you want to repalce all '-' with spaces and condense</b>


REPORT  zztest_arun_1 .

DATA : string TYPE string.

START-OF-SELECTION.
string = 'sdfsadf--asdf-asd-asd-'.

IF STRLEN( string ) GT 18.

  TRANSLATE string USING '- '.
  CONDENSE string NO-GAPS.

ENDIF.

WRITE :  string.

<b>If you want to repalce only '-' till length 18 form backwards with spaces and condense</b>


  REPORT  zztest_arun_2 .

  DATA : string TYPE string,
         len TYPE i,
         off TYPE i.

  START-OF-SELECTION.
    string = 'sdfsadf--asdf-asd-asd-'.

    len =  STRLEN( string ).

    IF len GT 18.

      DO.

        IF  STRLEN( string ) GT 18.

          off = len - sy-index.

          REPLACE '-' IN SECTION OFFSET off LENGTH -1 OF string WITH space.
          CONDENSE string NO-GAPS.


        ELSE.

          EXIT.

        ENDIF.

      ENDDO.

    ENDIF.

    WRITE :  string.

Regards,

Arun Sambargi

Message was edited by: Arun Sambargi

6 REPLIES 6
Read only

Former Member
0 Likes
744

Hi Jon,

Pls elaborate your requirement.

Regards

Shekhar

Read only

0 Likes
744

STRING = 'ABDCE'.

LEN = STRLEN( STRING ).

<b>if len > 18.</b>

use logic like that....

REPORT abc.

DATA : BEGIN OF itab OCCURS 0,

str(50),

END OF itab.

DATA : cha(50) VALUE 'cha-sek-jskjfsjf',

str(2000).

SPLIT cha AT '-' INTO TABLE itab.

loop at itab.

concatenate str itab-str into str separated by space.

endloop.

write : str.

Read only

madan_ullasa
Contributor
0 Likes
744

hi Jon,

try this.

data var_len type i, ch type c.

var_len = strlen of string.

if var_len > 18.

var_len = var_len - 1.

ch = string+var_len(1).

do var_len times.

if ch = '-'.

delete that character.

endif.

enddo.

this is just a rough logic... try this.

regards,

Madan..

Read only

Former Member
0 Likes
745

Hi Jon,

Consider these code.

<b>If you want to repalce all '-' with spaces and condense</b>


REPORT  zztest_arun_1 .

DATA : string TYPE string.

START-OF-SELECTION.
string = 'sdfsadf--asdf-asd-asd-'.

IF STRLEN( string ) GT 18.

  TRANSLATE string USING '- '.
  CONDENSE string NO-GAPS.

ENDIF.

WRITE :  string.

<b>If you want to repalce only '-' till length 18 form backwards with spaces and condense</b>


  REPORT  zztest_arun_2 .

  DATA : string TYPE string,
         len TYPE i,
         off TYPE i.

  START-OF-SELECTION.
    string = 'sdfsadf--asdf-asd-asd-'.

    len =  STRLEN( string ).

    IF len GT 18.

      DO.

        IF  STRLEN( string ) GT 18.

          off = len - sy-index.

          REPLACE '-' IN SECTION OFFSET off LENGTH -1 OF string WITH space.
          CONDENSE string NO-GAPS.


        ELSE.

          EXIT.

        ENDIF.

      ENDDO.

    ENDIF.

    WRITE :  string.

Regards,

Arun Sambargi

Message was edited by: Arun Sambargi

Read only

Former Member
0 Likes
744

Hi Jon

You can use the following code.

data: str(50 )type c.

data: len type i.

data: diff type i.

str = 'njiodjjjiojsjfgj----


'.

len = strlen( str).

diff = len - 18.

if diff > 0.

SHIFT str RIGHT DELETING TRAILING '-'.

endif.

I think this must solve out yor problem. If problem presists do reply.

Read only

0 Likes
744

hi.

and thanks to everyone helping!

this does not quite do what i need.

ie. i could get a string looking like this:

1234-678-12-34-56789

It's 19 char long and i need to remove the last '-' so that the string will be 1234-678-12-3456789

when testing all the possible solutions i'm only able to remove all the '-'.

i was thinking about writing the string in reverse to a temp_string and just replace first occ. of '-', but i cant find a easy way to reverse the string either...

/jon