cancel
Showing results for 
Search instead for 
Did you mean: 

Sub-string in a variable length SAP/ABAP

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Thanks so much both of you! 

The provided solutions  work although I’m not getting all records yet. But I guess this may have to do with some other issues, I will investigate.

Mr. Dibyendu Arya, can you please explain to me 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 ?

Also, while I was searching online, I ran into the following code examples with different substring functions. But when I tried them, I got an error saying that these functions were “private” or “protected”. I do understand the concept of private or protected functions and the fact that you have to define and initialize an instance of an object to be able to call private and protected functions.

My question to you guys experts, is these functions defined in Sap/Abap, and how one can take advantage of them and use them without having to write our own functions? 

Sorry for the questions if they don’t make lot sense to you experts, I’m new to Sap/abap, I’m trying to understand. Again big thanks to both you, you saved me a lot time, but most importantly, you helped to understand.

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' ).

Regards, James

Former Member
0 Kudos

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

Answers (1)

Answers (1)

markangelo_dihiansan
Active Contributor
0 Kudos

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