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 space in a string to underscore

sudha_naik
Product and Topic Expert
Product and Topic Expert
0 Likes
20,449

Hello All,

I would like to replace space in a string to an underscore.

Eg : 'LN MAT' should be changed to LN_MAT.

I tried this code

data : ls_text type mara-matnr.

REPLACE ALLOCCURRENCES OF SPACES

of ls_text with '_".

Runtime exception REPLACE_INFINITE_LOOP is thrown. Could anyone please help

me out as to what is the solution.

Thanks

Sudha

1 ACCEPTED SOLUTION
Read only

roland_knaupp1
Explorer
8,179

REPLACE ALL OCCURRENCES OF ` ` IN ls_text WITH '_'.

Please use Grave Accent ( ` ) instead of Apostrophe ( ' ), otherwise the expression ' ' will be handled like empty.

The expression ` ` will be handled as string.

13 REPLIES 13
Read only

Former Member
0 Likes
8,179

Hello,

Give me one example with the value from the table which you want to place _ in space.

Read only

sudha_naik
Product and Topic Expert
Product and Topic Expert
0 Likes
8,179

I want to change the string 'LN MAT' in to LN_MAT.

The string 'LN MAT' contain a space in between.

Read only

0 Likes
8,179

Hi,

U can use replace first occurance of ' ' with '_' instead of replace all occurances.

Regards,

Anju

Read only

0 Likes
8,179

Hello,

Try this.

DATA: y_v_text TYPE char10.

MOVE: 'LN MAT' TO y_v_text.

REPLACE ' ' WITH '_' INTO y_v_text.

WRITE:/ y_v_text.

Read only

Former Member
0 Likes
8,179

Hi,

check the code:

DATA:
  BEGIN OF wa,
    data(255),
  END OF wa.

DATA:
  itab LIKE STANDARD TABLE OF wa WITH HEADER LINE.

DATA:
  v_string1 TYPE string VALUE 'LV MAT',
  v_string2 type string.

SPLIT v_string1 AT space INTO TABLE itab.

loop at itab.
  concatenate v_string2 itab-data into v_string2 separated by '_'.
endloop.

shift v_string2 left deleting leading '_'.

write: / v_string2.

Thanks & Regards,

Navneeth K.

Read only

JozsefSzikszai
Active Contributor
8,179

just use TRANSLATE:

TRANSLATE string USING ' _'. "pls. note there is a space before the undersore

Read only

Former Member
0 Likes
8,179

Hi,

replace all occurrences of space

in <field> with '_' .

Thanks,

Krishna

Read only

Former Member
0 Likes
8,179

use this TRANSLATE abc USING ' _'.

note that inside the quotes we have space followed by _ which means replace space by _.

try sap help for translate for more info.

Read only

Former Member
0 Likes
8,179

Use IN in PLace of OF

REPLACE ALLOCCURRENCES OF SPACES IN ls_text with '_".

It will Work

Thanks

Saurabh

Read only

Former Member
0 Likes
8,179

Hello,

Please check if the material number is empty. Because the error that you are getting is one of the exceptions of the REPLACE...

Catchable Exceptions

CX_SY_REPLACE_INFINITE_LOOP

Cause: Substring of length 0 generates an endless loop when searching for all occurrences.

Runtime Error: REPLACE_INFINITE_LOOP (catchable)

Try to catch all the errors and find out the cause for it.

Thanks,

Jayant

Read only

Former Member
0 Likes
8,179

Hi Sudha

Tried at my end. The following code works :-

while sy-subrc eq 0.

replace space with '_' into ls_text .

endwhile.

Cheers !!

Neeraj

Read only

Former Member
0 Likes
8,179

A little late, but I just ran into this problem for the first time.  Another option I would suggest is:

REPLACE [ALL OCCURENCES OF] REGEX '\s' IN ls_text WITH '_'.

Read only

roland_knaupp1
Explorer
8,180

REPLACE ALL OCCURRENCES OF ` ` IN ls_text WITH '_'.

Please use Grave Accent ( ` ) instead of Apostrophe ( ' ), otherwise the expression ' ' will be handled like empty.

The expression ` ` will be handled as string.