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

String Manipulation

Former Member
0 Likes
1,839

I am quite new to abap and I am trying to produce some output. I have a string variable containing an address, but it stores the street name first and then the house number. I want the output to display the house number first. Any ideas on what code I would need to achieve this? i.e if the string contained "Moss Rd 26", I want the output to read "26 Moss Rd".

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,455

Below code is longer but it works even if apartment number is present.

String is reversed first, then regex is used for swapping, then string is reversed again for final output.


DATA input TYPE char100 VALUE 'Moss 12 Rd 26'.
DATA output TYPE char100.

DATA match TYPE match_result.
DATA submatch1 TYPE submatch_result.
DATA submatch2 TYPE submatch_result.

CALL FUNCTION 'STRING_REVERSE'
  EXPORTING
    string    = input
    lang      = sy-langu
  IMPORTING
    rstring   = input
  EXCEPTIONS
    too_small = 1
    OTHERS    = 2.
IF sy-subrc EQ 0.
  FIND FIRST OCCURRENCE OF REGEX '(\d*)(.*)' IN input RESULTS match.
  READ TABLE match-submatches INDEX 1 INTO submatch1.
  READ TABLE match-submatches INDEX 2 INTO submatch2.
  CONCATENATE input+submatch2-offset(submatch2-length)
              input+submatch1-offset(submatch1-length)
              INTO output SEPARATED BY space.

  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string    = output
      lang      = sy-langu
    IMPORTING
      rstring   = output
    EXCEPTIONS
      too_small = 1
      OTHERS    = 2.
  WRITE:/ output.
ENDIF.

12 REPLIES 12
Read only

Former Member
0 Likes
1,454

Hi,

you can use offset to fetch particular characters from string.Provided you know position of character.

Eg:

v_string = 'Moss Rd 26'.

v_house_no = v_string+2(7).

v_street = v_string+0(7).

Read only

0 Likes
1,454

Thanks Sap_Wiz, only problem is, this might not work if the house number is longer than 2 characters.

Read only

Former Member
0 Likes
1,454

Hi,

as these kind of data fields are pretty dynamic, the offsets have to be dynamic too. If it's given, that the house number is after the last blank, you can do this:


lf_s = 'Moss Rd 26'.

lf_i = STRLEN( lf_s ) - 1.
'--- where's the last blank?
DO.
  IF lf_i = 0. EXIT. ENDIF.
  IF lf_s+lf_i(1) = ' '. EXIT. ENDIF.
  lf_i = lf_i - 1.
ENDDO.

IF lf_i > 0.
'--- If there's any, split the string
  lf_l = STRLEN( lf_s ) - lf_i.
  WRITE: lf_s+lf_i(lf_l),
         lf_s(lf_i).
ENDIF.

Perhaps you can also add several check (numeric, length of field,...) for security reasons...

Kind Regards,

Dirk

Read only

0 Likes
1,454

Thanks, I have been able to use this, to form a solution.

Great help

Read only

0 Likes
1,454

Next time, please assign polnts to helpful answers.

Rob

Read only

0 Likes
1,454

Ok Rob, I have done it now, hopes its not too late.

Read only

0 Likes
1,454

It's never too late.

Rob

Read only

Former Member
0 Likes
1,454

What do you do if there's an apartment number?

Rob

Read only

Former Member
0 Likes
1,454

At first you need to determine how your string is built. Is there something which uniqueley idetifies the house number? Is it always after the last space? May it happen that after your housenumber something else is beeing added to that string?

If all those questions are answered, THEN you can think of how to do it.

Until then its more like "if you have A then doing B would be good, but if you have C doing D would be better".

Read only

0 Likes
1,454

Yes the house number is coming in always after the last space, so I am hoping as Dirk suggested, that establishing this last space should be able to isolate the house number.

Read only

Former Member
0 Likes
1,454

It works if apartment number is not there. Regex is used.


DATA input TYPE char100 VALUE 'Moss Rd 26'.
DATA output TYPE char100.

DATA match TYPE match_result.
DATA submatch1 TYPE submatch_result.
DATA submatch2 TYPE submatch_result.

FIND FIRST OCCURRENCE OF REGEX '(\D*)(\d*)' IN input RESULTS match.

READ TABLE match-submatches INDEX 1 INTO submatch1.

READ TABLE match-submatches INDEX 2 INTO submatch2.
CONCATENATE input+submatch2-offset(submatch2-length)
            input+submatch1-offset(submatch1-length)
            INTO output SEPARATED BY space.
WRITE:/ output.

Read only

Former Member
0 Likes
1,456

Below code is longer but it works even if apartment number is present.

String is reversed first, then regex is used for swapping, then string is reversed again for final output.


DATA input TYPE char100 VALUE 'Moss 12 Rd 26'.
DATA output TYPE char100.

DATA match TYPE match_result.
DATA submatch1 TYPE submatch_result.
DATA submatch2 TYPE submatch_result.

CALL FUNCTION 'STRING_REVERSE'
  EXPORTING
    string    = input
    lang      = sy-langu
  IMPORTING
    rstring   = input
  EXCEPTIONS
    too_small = 1
    OTHERS    = 2.
IF sy-subrc EQ 0.
  FIND FIRST OCCURRENCE OF REGEX '(\d*)(.*)' IN input RESULTS match.
  READ TABLE match-submatches INDEX 1 INTO submatch1.
  READ TABLE match-submatches INDEX 2 INTO submatch2.
  CONCATENATE input+submatch2-offset(submatch2-length)
              input+submatch1-offset(submatch1-length)
              INTO output SEPARATED BY space.

  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string    = output
      lang      = sy-langu
    IMPORTING
      rstring   = output
    EXCEPTIONS
      too_small = 1
      OTHERS    = 2.
  WRITE:/ output.
ENDIF.