‎2012 Jan 03 10:49 AM
Hi All,
I have a requirement where I need to replace the 'X' in the string.
EX: 1. ABCDXXX
2. ABXXCDXX
The output should be
1. ABCD
2. ABXXCD.
This mean I need to replace all the X which comes last in the string. There is a possiblity that X may come in between like Ex 2.
So that X should remain same.
What pattern I can give for this Or any other solution.
Kindly help.
‎2012 Jan 03 11:34 AM
If you are on ABAP kernel release 7 or above, then you should be able to do the magic with below two lines of code. This shows power and beauty of regular expressions
Replace X in 'X+$' with any character you wish to delete towards the end of string. The regular expression matches only trailing X and it won't touch any X in the middle
DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
REPLACE REGEX 'X+$' IN ls_string WITH ''.If you are below ABAP 7 then you can use the more mundane SHIFT command.
DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.
‎2012 Jan 03 10:59 AM
Try the following code
l
_i = strlen( p_string ).
L_I = L_I - 1.
while ( l_flag <> 'X' ).
if p_string+l_i(1) eq 'X'.
p_string+l_i(1) = ''.
l_i = l_i - 1.
else.
condense p_string.
l_flag = 'X'.
endif.
ENDWHILE.
write p_string.
‎2012 Jan 03 11:03 AM
do a reverse of the string => shift left untill the following character not equals X => reverse again
‎2012 Jan 03 11:12 AM
Hi Madan,
I tried implementing the logic you gave, but I am not getting proper results.
Here is code which I am working on , what can I write inside the LOOP so that the reults could be as told before:
TYPES: BEGIN OF ty_test,
col type char11,
END OF ty_test.
DATA: g_t_test type table of ty_test,
g_wa_test type ty_test.
DATA: len type i,
len1 type i.
g_wa_test-col = 'ABCDXXX'.
append g_wa_test to g_t_test.
clear: g_wa_test.
g_wa_test-col = 'ABXCDXXX'.
append g_wa_test to g_t_test.
clear: g_wa_test.
LOOP AT g_t_test INTO g_wA_test.
' What logic can we write here ' **********
WRITE:/ g_wa_test-col.
ENDLOOP.
‎2012 Jan 03 11:24 AM
Try this
LOOP AT g_t_test INTO g_wA_test.
l_i = strlen( g_wA_test-col ).
L_I = L_I - 1.
while ( l_flag <> 'X' ).
if g_wA_test-col+l_i(1) eq 'X'.
g_wA_test-col+l_i(1) = ''.
l_i = l_i - 1.
else.
condense g_wA_test-col.
l_flag = 'X'.
endif.
ENDWHILE.
write / g_wA_test-col.
clear l_flag.
ENDLOOP.Nabehet
‎2012 Jan 03 11:19 AM
Hi Madan,
I tried implementing the logic you gave, but I am not getting proper results.
Here is code which I am working on , what can I write inside the LOOP so that the reults could be as told before:
TYPES: BEGIN OF ty_test,
col type char11,
END OF ty_test.
DATA: g_t_test type table of ty_test,
g_wa_test type ty_test.
g_wa_test-col = 'ABCDXXX'.
append g_wa_test to g_t_test.
clear: g_wa_test.
g_wa_test-col = 'ABXCDXXX'.
append g_wa_test to g_t_test.
clear: g_wa_test.
LOOP AT g_t_test INTO g_wA_test.
' What logic can we write here ' **********
WRITE:/ g_wa_test-col.
ENDLOOP.
‎2012 Jan 03 11:33 AM
There is no operator in while ( l_flag 'X' ).
what operator should I use '='?
Suggest.
‎2012 Jan 03 11:34 AM
If you are on ABAP kernel release 7 or above, then you should be able to do the magic with below two lines of code. This shows power and beauty of regular expressions
Replace X in 'X+$' with any character you wish to delete towards the end of string. The regular expression matches only trailing X and it won't touch any X in the middle
DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
REPLACE REGEX 'X+$' IN ls_string WITH ''.If you are below ABAP 7 then you can use the more mundane SHIFT command.
DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.
‎2012 Jan 03 11:38 AM
‎2012 Jan 03 11:42 AM
I am sorry Vishnu that didnt work.
Nabheet how can use the while ( l_falg 'X' ) like this in my code. Its a syntax error. But the procedure is correct.
‎2012 Jan 03 11:45 AM
Sumana, I gave you two pieces of code and clearly mentioned that the first will work only in ABAP 7 or above (that is ECC 6 or above).
The below piece of code should work for you.
DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.Also when you say some code doesn't work, it would be a better to mention what the error is, so we can help you.
‎2012 Jan 03 11:54 AM
Sorry for that Vishnu. Actually there is no change in INPUT and OUTPUT both are same I am getting for the suggested code.
‎2012 Jan 03 11:59 AM
That is weird. Which SAP system are you on? Which release?
Both the pieces of code I gave you work on ECC 6 with ABAP 7
See if the below works.
ls_string should have 'ABXXCDXX' initially but after the SHIFT and CONDENSE it should become 'ABXXCD'
DATA: ls_string(30) TYPE c VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.
‎2012 Jan 03 12:31 PM
‎2012 Jan 05 7:12 AM
Basic programming question. Moved to Beginners' corner.
Moved back as resulted in interesting regex information...
‎2012 Jan 05 5:29 AM
Hi Vishnu,
Thats really splved my problem.
The only thing which confused me was my field type was CHAR11 for which it didnt work, but when I changed the type to STRING your code became the solution for it.
Thank You.