Simple method to wrap the subfield access for X and XSTRING variables

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

You May Also Like

More From Author