Application Development 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: 

ABAP Substring function from right side

Former Member

Hi,

I want a substring function in ABAP, which starts from right instead of left to display some character out of a complete string.

Suppose there is a variable zdatavar -

zdatavar = 'MyNameIsBush'.

zdatavar = zdatavar+4(2).

The above will start from the left.

I want some function which start from the right.

Your help will be greately appreciated.

Regards,

SC

1 ACCEPTED SOLUTION

mvoros
Active Contributor
0 Kudos

Hi,

there is no standad ABAP command which supports this. You need to get length of string with STRLEN and then convert it proper index. Another solution could be to reverse string and then use standard syntax.

Cheers

4 REPLIES 4

mvoros
Active Contributor
0 Kudos

Hi,

there is no standad ABAP command which supports this. You need to get length of string with STRLEN and then convert it proper index. Another solution could be to reverse string and then use standard syntax.

Cheers

Former Member

Hi Sume,

please see code below. just reuse function rsubstring according to your needs.


REPORT  ZTEST99.

DATA str TYPE STRING.
DATA str2 TYPE STRING.

str = 'MyNameIsBush'.
WRITE: / str.

PERFORM rsubstring
    USING
        str
        4
        2
    CHANGING
        str2
.

WRITE: / str2.


FORM rsubstring
    USING
        str TYPE STRING
        offset
        len
    CHANGING
        out TYPE STRING
.
    DATA:
        strln TYPE i,
        l_offset TYPE i,
        l_len TYPE i
    .

    strln = STRLEN( str ).
    l_offset = strln - offset - len.
    l_len = len.
    out = str+l_offset(l_len).
ENDFORM.

Former Member
0 Kudos

Hi,

Please check if this is working.


REPORT  Z0804.

DATA str TYPE STRING.
DATA str2 TYPE STRING.

str = 'MyNameIsBush'.
WRITE: / str.

PERFORM rsubstring
    USING
        str
        2
    CHANGING
        str2
.

WRITE: / str2.


FORM rsubstring
    USING
        str TYPE STRING
        len
    CHANGING
        out TYPE STRING
.
    DATA:
        strln TYPE i,
        l_offset TYPE i,
        l_len TYPE i
    .

    strln = STRLEN( str ).
    if strln GE len.

    l_offset = strln - len.
    l_len = len.
    out = str+l_offset(l_len).

    endif.
ENDFORM.

result:

MyNameIsBush

sh

regards,

Xiang Li

0 Kudos

Thank you for sugegstions.

Regards.