Application Development and Automation 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: 
Read only

Replace spaces in a string with Tab character

Flavio
Active Contributor
0 Likes
4,135

Dear all,

I'm looking for a way to replace all the spaces in a string, when they consequently occur more than one time, with a single 'Tab' character;

here a sample of the string

RAW MATERIAL TOOLS               82,000         59.083,200         52.062,420        148.121,834              0,000              0,000  

and the desired result:

RAW MATERIAL TOOLS'Tab'82,000'Tab'59.083,200'Tab'52.062,420'Tab'148.121,834'Tab'0,000'Tab'0,000  

is it possible to get the desired result with Regex?

any help is highly appreciated

thank you and best regards,

Flavio

1 ACCEPTED SOLUTION
Read only

MateuszAdamus
Active Contributor
3,351

Hello flavio.ciotola4

REPLACE ALL OCCURRENCES OF REGEX '[ ]{2,}' IN lv_string WITH cl_abap_char_utilities=>horizontal_tab.
Kind regards,
Mateusz

Dear all,

I'm looking for a way to replace all the spaces in a string, when they consequently occur more than one time, with a single 'Tab' character;

here a sample of the string

RAW MATERIAL TOOLS               82,000         59.083,200         52.062,420        148.121,834              0,000              0,000  

and the desired result:

RAW MATERIAL TOOLS'Tab'82,000'Tab'59.083,200'Tab'52.062,420'Tab'148.121,834'Tab'0,000'Tab'0,000  

is it possible to get the desired result with Regex?

any help is highly appreciated

thank you and best regards,

Flavio

5 REPLIES 5
Read only

Former Member
3,351

Hi, try this:

lv_string = replace( val = lv_string regex = '\s{2,}' with = cl_abap_char_utilities=>horizontal_tab occ = 0 ).
Read only

MateuszAdamus
Active Contributor
3,352

Hello flavio.ciotola4

REPLACE ALL OCCURRENCES OF REGEX '[ ]{2,}' IN lv_string WITH cl_abap_char_utilities=>horizontal_tab.
Kind regards,
Mateusz
Read only

3,351

Thank you mateuszadamus, it works!

Read only

0 Likes
3,351

OCCURANCES -> OCCURRENCES 😉

Auto-test code:

data(lv_string) = |RAW MATERIAL TOOLS               82,000         59.083,200         52.062,420        148.121,834              0,000              0,000  |.
REPLACE ALL OCCURRENCES OF REGEX '[ ]{2,}' IN lv_string WITH cl_abap_char_utilities=>horizontal_tab.
assert lv_string = |RAW MATERIAL TOOLS\t82,000\t59.083,200\t52.062,420\t148.121,834\t0,000\t0,000\t|.
Read only

3,351

Fixed the typo.


Kind regards,
Mateusz