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: 

add space in a string

Former Member
0 Kudos

Hi experts,

I have this problem: I split a string in a table, because I need to analyze each word in (I'm programmin a parser) and I want to write the word one by one, but I cannot find a way to write a space between them: the write is in a cycle:

LOOP AT word_table INTO word_wa.
   CONCATENATE word_wa space INTO word_wa SEPARATED BY space.
   WRITE word_wa.
ENDLOOP.

but it seems ignoring my instructions ...

So how can I add a space between the words?

Thanks in advance.

Gabriele

1 ACCEPTED SOLUTION

Former Member

Hi Gabriele,

Common problem. Instead of using SPACE, you need to ALT+255

You can use the same statement, don't necessary need the separated by clause:

CONCATENATE word_wa ' ' INTO word_wa.

But when you create the ' ' do not use the space bar. Press ', then press Alt key+pressNumpadkey255 to create the space (you will see it get created), then press ' again. I am betting this will work.

Good luck.

SL

28 REPLIES 28

Former Member

Try

CONCATENATE word_wa space INTO word_wa RESPECTING BLANKS.

Regards,

Mohaiyuddin

Former Member
0 Kudos

use SPLIT command.

Former Member
0 Kudos

hi ,

use SPLIT

Also Check this link for SPLIT ..

http://help.sap.com/saphelp_46c/helpdata/EN/fc/eb3400358411d1829f0000e829fbfe/frameset.htm

Regards,

Sachin

Former Member
0 Kudos

Try Using

LOOP AT word_table INTO word_wa.

CONCATENATE word_wa '' INTO word_wa1 SEPARATED BY SPACE.

ENDLOOP.

WRITE word_wa1.

Regards,

Prashant

Former Member
0 Kudos

Dear friend,

EXAMPLE:

BEGIN OF WORD_TABLE,

A(10) TYPE C,

B(10) TYPE C,

END OF WORD_TABLE.

data : WORD_WA_TEMP TYPE STRING.

Word_wa having 3 or 4 fields example.

LOOP AT word_table INTO word_wa.

CONCATENATE WORD_WA-A WORD_WA-B INTO WORD_WA_TEMP SEPARATED BY SPACE.

WRITE WORD_WA_TEMP .

ENDLOOP.

Former Member
0 Kudos

Hello,

Use SPLIT command.

regards,

Vivek

Former Member
0 Kudos

CONCATENATE word_wa 'space' into word_wa SEPARATED BY SPACE.

Regards,

Ajay

rainer_hbenthal
Active Contributor

Hi,

i you use strings instead of char varaibles you can have spaces at the end.

Literals have to be enclosed in back quotes instead of apostrophes to make them strings.


concatenate `A` ` ` `B` into s.

will have another result as


concatenate 'A' ' ' 'B' into s.

In the above case s will contain a space between A and B using backquotes, even if s is only a char variable.


concatenate `A` ` ` `B` ` ` into s.

In that case, s will contain a space between A and B and a space at the end if s is declared as string, and it will contain a space between A and B, but now space at the end if it is declared as c.

Give it a try, it will make life easier if you are dealing with spaces, without always using the separeted by clause.

Former Member
0 Kudos

Hi,

Just check this code.

DATA: BEGIN OF word_table OCCURS 0,

a1 TYPE string,

END OF word_table.

DATA: word_wa LIKE word_table-a1.

DATA: a2 TYPE string.

word_table-a1 = 'nitesh1'.

APPEND word_table.

word_table-a1 = 'nitesh2'.

APPEND word_table.

LOOP AT word_table.

word_wa = word_table-a1.

CONCATENATE word_wa space INTO a2 SEPARATED BY space.

WRITE a2.

CLEAR a2.

ENDLOOP.

OUTPUT:  nitesh1  nitesh2

Thanks

NK

Former Member
0 Kudos

Hi Gabriele,

Hope the below may help you.

DATA: BEGIN OF t_itab OCCURS 0,

char1 TYPE string,

END OF t_itab.

DATA: w_itab LIKE LINE OF t_itab,

string1 TYPE string,

flg TYPE char1.

w_itab-char1 = 'First'.

APPEND w_itab TO t_itab.

w_itab-char1 = 'Second'.

APPEND w_itab TO t_itab.

w_itab-char1 = 'Third'.

APPEND w_itab TO t_itab.

CLEAR: string1.

LOOP AT t_itab INTO w_itab.

AT FIRST.

flg = 'X'.

ENDAT.

IF flg IS INITIAL.

CONCATENATE string1 w_itab-char1 INTO string1 SEPARATED BY space.

ELSE.

string1 = w_itab-char1.

CLEAR: flg.

ENDIF.

ENDLOOP.

WRITE:/ string1.

Regards,

Ramkumar.K

Former Member
0 Kudos

Hello,

You can write your whole code some what like below also.

data: word_wa2 type <same as word_wa>.

LOOP AT word_table INTO word_wa.

if word_wa2 is initial.

word_wa2 = word_wa.

continue.

endif.

CONCATENATE word_wa word_wa2 INTO word_wa2 SEPARATED BY space.

word_wa2 = word_wa.

ENDLOOP.

WRITE word_wa2.

Hope it helps.

Thanks,

Jayant

Former Member
0 Kudos

Hi Gabriele,

try it this way:

DATA:
  string type string,
  char(2) value '  '.  " take it as you want the gap to appear

LOOP AT word_table INTO word_wa.
   CONCATENATE string char word_wa INTO string respecting blanks.
   WRITE string.
ENDLOOP.

With luck,

Pritam.

former_member203501
Active Contributor
0 Kudos

hi look at this ...

0 Kudos

Thanks to everybody,

but I cannot merge words in a new string: this is a source parser and each word found had to be stored on a file by itself. I can put a space after each word found, but number of operations duplicate in this way ...

I can't believe it's so hard to put a space at the end of a string. Statement like:

char c1(1) TYPE c VALUE ' '.
CONCATENATE word c1 into word.

CONCATENATE word space INTO word SEPARATED BY space.

CONCATENATE word space INTO word RESPECTING BLANKS SEPARATED BY space.

CONCATENATE word space INTO word RESPECTING BLANKS.

does not work ... ant the wonderful thing is that if I want to put a leading space instead of a trailing one, it works.

Former Member
0 Kudos

Hi Gabriele,

the problem is concatenate deletes spaces. try this:

LOOP AT word_table INTO word_wa.

CONCATENATE word_wa '~' INTO word_wa SEPARATED BY space.

WRITE word_wa.

ENDLOOP.

TRANSLATE word_wa USING '~ '.

Hope it helps.

Regards, Dieter

Former Member

Hi Gabriele,

Common problem. Instead of using SPACE, you need to ALT+255

You can use the same statement, don't necessary need the separated by clause:

CONCATENATE word_wa ' ' INTO word_wa.

But when you create the ' ' do not use the space bar. Press ', then press Alt key+pressNumpadkey255 to create the space (you will see it get created), then press ' again. I am betting this will work.

Good luck.

SL

0 Kudos

perfect ... I was sure there was a quick trick ... without thousands of lines of code ... it's impossible to write an external program to add a space in a string in a program!

thank you very much!

cheers

Gabriele

0 Kudos

This message was moderated.

0 Kudos

Hi Shane,

Thanx a lot for your solution!!!!!!

Best regards

Willi Eimler

Use the && operator:

String = String1 && ` ` && string2

Note that the ` marks are not apostophes but the quote marks under the ¬ key.

Rich

Former Member
0 Kudos

Hi Gabriele,

Try this code and do accordingly.

REPORT ZTEST123A.

DATA: z(34),a,b(10),l(25).

DATA:e(2).

DATA:x(256) ,y(256), l_var TYPE i.

x = 'GabrieleMontori '.

DATA w TYPE STANDARD TABLE OF string.

l_var = STRLEN( x ).

DATA:lv TYPE i, m type i.

DO l_var TIMES.

lv = sy-index.

y = x+m(1).

CONCATENATE y ' ' INTO z .

MOVE: z to b.

CONCATENATE b ' ' INTO l.

m = m + 1.

APPEND z to w.

ENDDO.

Loop at w INTO e.

WRITE: e NO-GAP.

CONCATENATE e ' ' INTO e.

ENDLOOP.

Thanks

Rasheed

0 Kudos

works like a gem! thanks!

Former Member
0 Kudos

USE SPLIT COMMAND

SEE THE FOLLOWING EXAMPE

DATA: STRING(60),

P1(20) VALUE '++++++++++++++++++++',

P2(20) VALUE '++++++++++++++++++++',

P3(20) VALUE '++++++++++++++++++++',

P4(20) VALUE '++++++++++++++++++++',

DEL(3) VALUE '***'.

STRING = ' Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5'.

WRITE STRING.

SPLIT STRING AT DEL INTO P1 P2 P3 P4.

WRITE / P1.

WRITE / P2.

WRITE / P3.

WRITE / P4.

OUTPUT IS:-

*Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5

*

*Part 1

*

*Part 2

*

*Part 3

*

*Part 4 *** Part 5

Former Member
0 Kudos

Hi Gabriele,

Did ALT+255 not work?

SL

0 Kudos

Hi

I don't use the ALT+255. I have a US keyboard and use the "reverse apex" ( ` )for the space.

CONCATENATE a b INTO c SEPARATED by space. not working

CONCATENATE a b INTO c SEPARATED by ' '.  not working

CONCATENATE a b INTO c SEPARATED by ` `. working

Former Member
0 Kudos

Hi,

I've used:

CONCATENATE lv_String1 lv_String 2 INTO lv_Result SEPARATED BY space.

and it worked for me.

I've used this in a transformation in SAP BW.

Regards.

Former Member
0 Kudos

This message was moderated.

20enzo
Explorer
0 Kudos

Hi all.

This is a kind update on possible solutions for this problem.

The aim here is to achieve exactly the same result on some possible solution.

DATA word_table TYPE STANDARD TABLE OF string.

APPEND 'This' TO word_table.
APPEND 'is' TO word_table.
APPEND 'a' TO word_table.
APPEND 'test' TO word_table.

DATA:
word_wa TYPE string,
result TYPE string.

*--------------------------------------------------------------------*
*Solution 01 - Use two variables to avoid overwriting the same value
*--------------------------------------------------------------------*
LOOP AT word_table INTO word_wa.
IF sy-tabix = 1.
result = word_wa.
ELSE.
CONCATENATE result word_wa INTO result SEPARATED BY space.
ENDIF.
ENDLOOP.
WRITE: / 'Solution 01 - ', result.

*--------------------------------------------------------------------*
*Solution 02 - Use && and ` `
*--------------------------------------------------------------------*
LOOP AT word_table INTO word_wa.
IF sy-tabix = 1.
result = word_wa.
ELSE.
result = result && ` ` && word_wa.
ENDIF.
ENDLOOP.
WRITE: / 'Solution 02 - ', result.

*--------------------------------------------------------------------*
*Solution 03 - Use "|"
*--------------------------------------------------------------------*
LOOP AT word_table INTO word_wa.
IF sy-tabix = 1.
result = word_wa.
ELSE.
result = |{ result } { word_wa }|.
ENDIF.
ENDLOOP.
WRITE: / 'Solution 03 - ', result.

*--------------------------------------------------------------------*
*Solution 04 - Use "lines of" on concatenate
*--------------------------------------------------------------------*
CONCATENATE LINES OF word_table INTO result SEPARATED BY space.
WRITE: / 'Solution 04 - ', result.

*--------------------------------------------------------------------*
*Solution 05 - Use standard function "concat_lines_of"
*--------------------------------------------------------------------*
result = concat_lines_of( table = word_table sep = ` ` ).
WRITE: / 'Solution 05 - ', result.