Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to replace the nth position value in a word ?

0 Kudos

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

1 ACCEPTED SOLUTION
10 REPLIES 10

hubert_heitzer
Contributor
0 Kudos

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

matt
Active Contributor
0 Kudos

He means the sixth "word", in a comma delimited string.

0 Kudos

Ah sorry, my crystal ball just got broken today morning.

Which model do you use Matt?

matt
Active Contributor
0 Kudos

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

matt
Active Contributor

Look up the keyword "SPLIT" and look at your course notes on how to concatenate strings.

horst_keller
Product and Topic Expert
Product and Topic Expert

Hmm, SPLIT and CONCATENATE for REPLACEing (and a WHILE loop for ALL OCCURRENCES)?

Done that before 7.0, but refactored now.

matt
Active Contributor

I hadn't had my first coffee...

Jelena
Active Contributor
0 Kudos

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.

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