‎2011 Jul 07 11:53 AM
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".
‎2011 Jul 07 3:02 PM
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.
‎2011 Jul 07 12:04 PM
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).
‎2011 Jul 07 1:15 PM
Thanks Sap_Wiz, only problem is, this might not work if the house number is longer than 2 characters.
‎2011 Jul 07 12:21 PM
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
‎2011 Jul 07 3:34 PM
Thanks, I have been able to use this, to form a solution.
Great help
‎2011 Jul 07 3:43 PM
‎2011 Jul 07 3:53 PM
‎2011 Jul 07 4:00 PM
‎2011 Jul 07 2:14 PM
‎2011 Jul 07 2:29 PM
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".
‎2011 Jul 07 2:35 PM
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.
‎2011 Jul 07 2:53 PM
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.
‎2011 Jul 07 3:02 PM
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.