The SUBSTRING function works only on CLIKE variables (C, D, N, T and STRING), not on X and XSTRING variables.
For X and XSTRING variables, the only solution is the subfield access DATAOBJECT+OFF(LEN).
This construct however doesn’t allow using an expression for any of the three arguments (data object, offset or length).
For instance, I’d like a function SUBSTRING_X which allows this writing:
Â
IF start = substring_x( val = xstring_table[ 1 ]
off = 0
len = xstrlen( start ) ).
…
Â
So, here is the solution:
Â
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
CLASS-METHODS substring_x
IMPORTING val TYPE xsequence
!off TYPE i DEFAULT 0
!len TYPE i DEFAULT -1
RETURNING VALUE(result) TYPE xstring
RAISING cx_sy_range_out_of_bounds.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD substring_x.
IF len = -1.
result = val+off.
ELSE.
result = val.
result = result+off(len).
ENDIF.
ENDMETHOD.
ENDCLASS.
Â
Thanks for reading this short post.
Sandra
Â
â The SUBSTRING function works only on CLIKE variables (C, D, N, T and STRING), not on X and XSTRING variables.For X and XSTRING variables, the only solution is the subfield access DATAOBJECT+OFF(LEN).This construct however doesn’t allow using an expression for any of the three arguments (data object, offset or length).For instance, I’d like a function SUBSTRING_X which allows this writing: IF start = substring_x( val = xstring_table[ 1 ]
off = 0
len = xstrlen( start ) ).
…Â So, here is the solution:Â CLASS lcl_app DEFINITION.
PUBLIC SECTION.
CLASS-METHODS substring_x
IMPORTING val TYPE xsequence
!off TYPE i DEFAULT 0
!len TYPE i DEFAULT -1
RETURNING VALUE(result) TYPE xstring
RAISING cx_sy_range_out_of_bounds.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD substring_x.
IF len = -1.
result = val+off.
ELSE.
result = val.
result = result+off(len).
ENDIF.
ENDMETHOD.
ENDCLASS. Thanks for reading this short post.Sandra   Read More Technology Blogs by Members articlesÂ
#SAP
#SAPTechnologyblog