Skip to Content
avatar image
Former Member

Sub-string in a variable length SAP/ABAP

Hi

I have a variable size string with max len sequels to 10. Regarding of the size of the data, it ends with ABC.

I'd like to substring the string before the pattern ABC. How do I achieve this in SAP/ABP. Below is what I was trying which did not work:

samples data source string (myString ): "fafaABC" "lABC" "ruiqiqrABC"

The output I need is : fafa, i , ruiqiqr

Thanks.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 25, 2013 at 03:03 AM

    Hi James,

    Please do your ABAP code as following.

    You are looking for a particular pattern in a string, so in your case it is 'ABC' and you want string before pattern 'ABC'.This will work irrespective of position of 'ABC'.Whether 'ABC' is in middle or end. i.e 'fafaABC' or 'fafaABCxyz'.To understand it just debug the following code once and look at the content of internal table git_match_result_tab.

    DATA:p_sch type string,

    gv_input type string,

    gv_pos type syfdpos,

    gv_string_pre type string.

    DATA:git_match_result_tab type match_result_tab,

    wa_match_result type match_result.

    p_sch = 'ABC'.

    gv_input = 'fafaABC'.

    FIND ALL OCCURENCES OF REGEX p_sch in gv_input

    RESULT git_match_result_tab.

    read table git_match_result_tab into wa_match_result index 1.

    if sy-subrc eq 0.

    gv_pos = wa_match_result-offset.

    gv_string_pre = gv_input+0(gv_pos).

    write gv_string_pre.

    endif.

    Please dont forget to reward points if it is helpful.😊

    Regards,

    Dibyendu Arya

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi James,

      Good to see that it has helped you.For your question 'why we still need to define and use these two variables (git_match_result_tab wa_match_result) of type match_result_tab and match_result ?'

      Answer is: git_match_result_tab is an internal table it is meant to hold one or more entries at a time.Now say we have across a scenario where we have to find a pattern 'ABCD' in a string and this particular pattern 'ABCD' is at multiple places in same string e.g.

      'fafaABCDefghABCDmbndhdhABCD'.So in this case internal table git_match_result_tab will contain three line for each occurence of 'ABCD'. Now you can loop on this internal table and use work area wa_match_result to know each position of a pattern and do your operations accordingly.

      I tried using the various options which you mentioned in follwoing examples.

      result = substring( val = 'ABCDEFGH' off = 2 len = 2 ).

      result = substring_from( val = 'ABCDEFGH' sub = 'CD' ).

      result = substring_after( val = 'ABCDEFGH' sub = 'CD' ).

      result = substring_before( val = 'ABCDEFGH' sub = 'CD' ).

      result = substring_to( val = 'ABCDEFGH' sub = 'CD' ).

      But these are working as expected for me and I did not get any error of 'PRIVATE' or 'PROTECTED'. May be it has got to do with something else.If you could provide more input I may be of some help.You are new to ABAP shoul not be a problem at all, you will catch it very soon.

      Regards,

      Dibyendu Arya

  • Jan 25, 2013 at 03:54 AM

    Hello,

    If it always end in ABC, you can SHIFT the string by 3 places right circular and then offset by 3 characters 😊

    DATA: gv_source TYPE STANDARD TABLE OF STRING,
    lv_dat TYPE STRING,
    lv_out TYPE STRING.

    lv_dat = 'fafaABC'.
    APPEND lv_dat TO gv_source.

    lv_dat = 'IABC'.
    APPEND lv_dat TO gv_source.

    lv_dat = 'ruiqiqrABC'.
    APPEND lv_dat TO gv_source.

    LOOP AT gv_source INTO lv_out.
    SHIFT lv_out BY 3 PLACES RIGHT CIRCULAR.
    lv_out = lv_out+3.
    WRITE: lv_out.
    ENDLOOP.

    Hope this helps,

    Mark

    Add comment
    10|10000 characters needed characters exceeded