Skip to Content
avatar image
Former Member

ABAP Substring function from right side

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

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Best Answer
    Aug 03, 2010 at 11:51 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 04, 2010 at 02:52 AM

    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.
    

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 04, 2010 at 03:34 AM

    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

    Add comment
    10|10000 characters needed characters exceeded