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: 

DUMP: replace all occurrences of space in string1 with string2.

norbert_weber
Explorer
0 Kudos

Why does this statement results in a dump:

replace all occurrences of space in string1 with string2.

same with

replace all occurrences of ' ' in string1 with string2.

string2 is a string without spaces!

Is there any drawback on using this statements as a workaround?

while sy-subrc eq 0.

replace space with string2 into string1.

endwhile.

Thanks

norbert

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Norbert: does the character string you are using to replace with spaces contain spaces?

Rob

17 REPLIES 17

athavanraja
Active Contributor
0 Kudos

Is string2 is of type string? change it to type C.

Regards

Raja

Former Member
0 Kudos

Hi,

Check this out

Addition

... LENGTH len ... (length specification for field f)

Effect

Searches for the string f in the field h not in its defined field length, but in the length len.

Example

DATA: pattern(5) VALUE 'ABC',

len TYPE I,

repl_string(5) VALUE '12345',

field(12) VALUE 'abcdeABCDE'.

REPLACE pattern WITH repl_string

INTO field.

does not change FIELD, since 'ABC ' does not occur in abcdeABCDE '.

LEN = STRLEN( PATTERN ).

REPLACE PATTERN LENGTH LEN

WITH REPL_STRING

INTO FIELD.

<b>changes FIELD to 'abcde12345D'.</b>

Also see this link

Thanks & Regards,

Judith.

0 Kudos

Thanks to all,

@Raja: string1/2 already type c

@Jayanthi: don't understand your answer

@Judith:

You are right,

I thought there are no spaces in string2, but yes, there are a lot of them after strlen(string2).

But <b>still I get this infinite loop dump</b>.

DATA: lc_Text(1000),

li_len TYPE i,

li_off TYPE i.

lc_Text = 'text with spaces'.

li_len = STRLEN( lc_Text ).

li_off = 0.

REPLACE ALL OCCURRENCES OF space

IN SECTION OFFSET li_off LENGTH li_len

OF lc_Text WITH '!$@!'.

Thank You

Regards

norbert

0 Kudos

then use <b>condense</b> string2 <b>no-gaps</b> option. this would remove the trailing spaced.

key word documentation for <b>condense</b>.

In the variable text, leading and closing blanks are completely removed and any other directly consecutive blanks are all replaced by exactly one space character or - if NO-GAPS is specified - are also removed completely.

The data object text must be character-type. If the data object has a fixed length, any space created by the condense operation is filled with blanks on the right. If the data object is of the type string, its length is adapted to the result of the condense operation.

Regards

Raja

0 Kudos

Thanks Raja,

but somehow, I was on the wrong track.

This statement provokes an infinite loop dump and I've no idea why?

DATA: lc_Text(1000).

lc_Text = 'text with spaces'.

REPLACE ALL OCCURRENCES OF space

IN lc_Text WITH '!$@!'.

Thank you

norbert

Message was edited by: Norbert Weber

0 Kudos

Hi Norbert,

if you check the dump message, the reason for the dump is, that SPACE gets interpreted as a field of length 0. And this will lead to an endless loop.

So I think you need to let us know what you will achieve with this statement. Is it just to have '!$@!' for 250 times in your field lc_text? Then it might be better to do it in a loop like this:

do 250 times.

concatenate '!$@!' lc_text into lc_text.

*write:/ lc_text.

enddo.

regards

Siggi

Message was edited by: Siegfried Szameitat

0 Kudos

Thank You Siggi,

a character variable lc_text holds an unknown text of length strlen( lc_text ).

In this section (not till the defined length of lc_text) I want to replace an unknown number of spaces (' ') each time into a certain character string.

Regards

norbert

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

Regarding your question about while,

I think you are using both string1 and string2 as variables.There is no point to use a loop there.

Former Member
0 Kudos

Norbert: does the character string you are using to replace with spaces contain spaces?

Rob

0 Kudos

Hi,

See this example i got from ABAPDOCU

  • replacing values

DATA: t4(10) TYPE c VALUE 'abcdefghij',

string4 LIKE t4,

str41(4) TYPE c VALUE 'cdef',

str42(4) TYPE c VALUE 'klmn',

str43(2) TYPE c VALUE 'kl',

str44(6) TYPE c VALUE 'klmnop',

len4 TYPE i VALUE 2.

string4 = t4.

WRITE string4.

REPLACE str41 WITH str42 INTO string4.

WRITE / string4.

string4 = t4.

REPLACE str41 WITH str42 INTO string4 LENGTH len4.

WRITE / string4.

string4 = t4.

REPLACE str41 WITH str43 INTO string4.

WRITE / string4.

string4 = t4.

REPLACE str41 WITH str44 INTO string4.

WRITE / string4.

SKIP.

ULINE.

Example for condensing strings

DATA: string9(25) TYPE c VALUE ' one two three four',

len9 TYPE i.

len9 = strlen( string9 ).

WRITE: string9, '!'.

WRITE: / 'Length: ', len9.

CONDENSE string9.

len9 = strlen( string9 ).

WRITE: string9, '!'.

WRITE: / 'Length: ', len9.

CONDENSE string9 NO-GAPS.

len9 = strlen( string9 ).

WRITE: string9, '!'.

WRITE: / 'Length: ', len9.

SKIP.

ULINE.

Thanks & Regards,

Judith.

0 Kudos

check out this option.

DATA: lc_Text(1000).

lc_Text = 'text with spaces'.

shift lc_text right deleting trailing space.

translate lc_text using ' ~' .

shift lc_text left deleting leading '~' .

REPLACE ALL OCCURRENCES OF '~'

IN lc_Text WITH '!$@!'.

Regards

Raja

0 Kudos

Thank You all for your help, I very appreciate that.

@Rob: No, there are no spaces in the replacestring, it's a literal like '!$@!'

@Judith: Thank You, but this docu doesn't really helps me.

@Raja: Actually that's what I wanted to achieve. But I wanted to use a more seldom literal than yours '~'.

What happens if the text already contains one or more '~'?

I have a text with an unknown number of spaces. Some spaces need to be replaced into a entity '&VGN;'.

To identify these spaces, I have some keywords:

some with an space in front and some with an space behind.

e.g.: 'fm. ' or ' HR'

First I wanted to replace all spaces into an 'never occur literal'. After that I could easily replace the keywords with an space behind.

I thought 'replace all occurrence of space in text with 'never occur' is a good idea to do that, but this gives a dump because space gets interpreted as length 0.

Any ideas?

Thanks a lot.

Regards

norbert

0 Kudos

Hi Norbert,

may be you should create your own function module or subroutine or method for this containing some code similar to this:

DATA: lc_text(1000) VALUE 'this is a test'.

DATA: l_position TYPE i.

DATA: l_strlen TYPE i.

l_position = 0.

DO 1000 TIMES.

l_strlen = STRLEN( lc_text ).

IF l_strlen <= l_position.

EXIT.

ENDIF.

IF lc_text+l_position(1) = space.

REPLACE lc_textl_position(1) IN lc_textl_position WITH '!$@!'.

IF sy-subrc = 0.

WRITE: / lc_text.

l_position = l_position + 4.

SHIFT lc_text+l_position BY 1 PLACES LEFT.

WRITE: / lc_text.

ENDIF.

ENDIF.

l_position = l_position + 1.

ENDDO.

You can easily copy it to a test program and check it out. It is some kind of quick and dirty solution but I believe you can easily adjust it to your needs.

regards

Siggi

Message was edited by: Siegfried Szameitat

0 Kudos

Hello Siggi,

that's a nice workaround, thank You very much.

Regards

norbert

0 Kudos

Hi,

Please close this thread if ur problem is solved.

0 Kudos

Use Translate statement. Only 8 years late but useful if anyone's still looking.

0 Kudos

Well... it would have been useful if you'd posted it yesterday when I encountered this problem.