08-09-2018 3:51 AM
Hi All,
How to replace the nth position field.
I can get the position of the field by using Find.I want to replace that position field with a variable.
For Example:- "ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,uahsuah,iwueiwue,jueuwhgd".
I want to replace the 6th position value with aaaaa
The result should be like:-
ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,aaaaa,iwueiwue,jueuwhgd
Thank you in advance!
Regards,
Sri Harika
08-09-2018 12:03 PM
08-09-2018 6:06 AM
Hi Sri,
the keyword WRITE should help you.
E.g.
DATA:
lv_source TYPE text72 VALUE 'ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,uahsuah,iwueiwue,jueuwhgd',
lv_pos TYPE i VALUE 6,
lv_len TYPE i,
lv_subst TYPE string VALUE 'aaaaa'
.
WRITE: / lv_source.
lv_len = strlen( lv_subst ).
lv_pos = lv_pos - 1.
WRITE lv_subst TO lv_source+lv_pos(lv_len).
WRITE: / lv_source.
Regards, Hubert
08-09-2018 6:51 AM
He means the sixth "word", in a comma delimited string.
08-09-2018 6:58 AM
Ah sorry, my crystal ball just got broken today morning.
Which model do you use Matt?
08-09-2018 7:05 AM
I just looked at the before and after example:
Before: ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,uahsuah,iwueiwue,jueuwhgd
After: ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,aaaaa,iwueiwue,jueuwhgd
08-09-2018 6:50 AM
Look up the keyword "SPLIT" and look at your course notes on how to concatenate strings.
08-09-2018 3:10 PM
Hmm, SPLIT and CONCATENATE for REPLACEing (and a WHILE loop for ALL OCCURRENCES)?
Done that before 7.0, but refactored now.
08-09-2018 5:04 PM
08-09-2018 12:03 PM
08-09-2018 3:27 PM
Since I was not the only one confused by this, just want to point out that the example has multiple words in it (the word ends after a punctuation mark). This is about replacing the Nth word in a string, not the Nth position in a word.
09-18-2018 9:15 AM
Hi All,
Thanks a lot for your Answers!
I just want to mention that how I have done this so that It may helpfull in future for others.
I have used "segment" to get the particular field at comma to know the length of that position.
DATA(l_rec) = segment( val = "ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,uahsuah,iwueiwue,jueuwhgd" index = 6 sep = lc_comma ).
DATA(l_rec_len) = strlen( l_rec ).
And getting the position of comma by using find option:-
DATA(l_rec_pos) = find( val = "ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,uahsuah,iwueiwue,jueuwhgd" sub = lc_comma occ = 6 ).
Then I used Replace option:-
REPLACE SECTION OFFSET l_rec_pos LENGTH l_rec_len OF:
"ababap,ihaidhaidha,yusgdusd,hsjdhus,uhadjahd,uahsuah,iwueiwue,jueuwhgd" WITH 'aaaa'.
Hope this solution dosen't create confusion...
Thank You,
Sri Harika