Skip to Content
-1

How to get rightmost characters in a string after "-" in ABAP?

I have a field which contains text of employee subgroup. For Eg.

1. MD-Exec-MD

2. DR-Exec-Directors

From the above text I want the rightmost part of the text after 2nd "-". Can anyone suggest something. Idea will be appreciated.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Jun 12, 2017 at 11:56 AM
    l_Ds = SubString( Val = 'DR-Exec-Directors'
                      Off = Find( Val   = 'DR-Exec-Directors'
                                  Regex = '-[^-]+$' ) + 1 ).
    Add comment
    10|10000 characters needed characters exceeded

  • Jun 12, 2017 at 07:34 PM
    third_segment = segment( val = 'DR-Exec-Directors' index = 3 sep = '-' ).
    ASSERT third_segment = 'Directors'.
    Add comment
    10|10000 characters needed characters exceeded

  • Jun 12, 2017 at 12:21 PM

    Another options just for fun:

    data(result) = match( val   = 'DR-Exec-Directors'  
                          regex = '[^-]*$' ).
    Add comment
    10|10000 characters needed characters exceeded

  • Jun 12, 2017 at 01:53 PM

    While I prefer Mike Pokraka's and Richard Harper's answers - they will be better performance-wise - if you don't have a recent version of ABAP, here is an older approach.

    DATA:
      substring_tab TYPE STANDARD TABLE OF string,
      last_substring TYPE string,
      num_lines TYPE i.
    
    SPLIT 'DR-Exec-Directors' AT '-' INTO TABLE substring_tab IN CHARACTER MODE.
    DESCRIBE TABLE substring_tab LINES num_lines.
    
    READ TABLE substring_tab INTO last_substring INDEX num_lines.
    
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 12, 2017 at 10:45 AM

    Hi Badal,

    Check my code for u r requirement..

    SPAN {
    font-family: "Courier New";
    font-size: 10pt;
    color: #000000;
    background: #FFFFFF;
    }
    .L0S32 {
    color: #3399FF;
    }
    .L0S33 {
    color: #4DA619;
    }
    .L0S52 {
    color: #0000FF;
    }
    .L0S55 {
    color: #800080;
    }
    .L0S70 {
    color: #808080;
    }
    
    DATA id type STring VALUE 'DR-Exec-Directors'.
    
    
    
    find  ALL OCCURRENCES OF '-' IN id IGNORING CASE  MATCH COUNT data(id1).
    
    CHECK sy-subrc eq 0 and id1 = 2.
    
    SPLIT id AT '-' INTO DATA(str1) DATA(str2) data(str3).
    
    
    
    WRITE : 'The right most part of string',id,'is',':', str3 .
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 12, 2017 at 01:54 PM

    Hi

    I will write as below:

    which works perfectly:

    Data a type string.
    
    a =  'MD-Exec-MD'.
    
    data: result_tab type  MATCH_RESULT_TAB.
    
    find  ALL OCCURRENCES OF '-' in a
    
    RESULTS result_tab.
    
    

    the last line of result_tab will have the answer

    Add comment
    10|10000 characters needed characters exceeded