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

ABAP Substring function from right side

Former Member
23,588

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
Read only

mvoros
Active Contributor
0 Likes
7,674

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
Read only

mvoros
Active Contributor
0 Likes
7,675

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

Read only

Former Member
7,674

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.

Read only

Former Member
0 Likes
7,674

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

Read only

0 Likes
7,674

Thank you for sugegstions.

Regards.