Sub-string in a variable length SAP/ABAP
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
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
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).
Please dont forget to reward points if it is helpful.