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

string Concatenate issue

sanjana_lingras
Active Participant
0 Likes
3,341

Hi Experts,

I am having issue while concatenating string inside loop.

Below is the scenario,

Inside loop w_string is getting appended,like

loop on it_line.

   append it_line-v_str to w_string.

endlloop.

Now w_string will have sequence of values like for e.g. in first iteration w_stinrg = 'hello   ' , 2nd interation " w_string = 'hello    how', 3rd iteration : w_string = 'hello    how    are', 4th iteration of loop w_string  = 'hello    how     are     you' ...likeways.

This w_string is getting appended with new value depending upon offset and length,

Now I need to write concatenate inside this loop , to concatenatenate this w_string value into another string w_string1 considering offset without repeating  same values. so my w_string1 will have same content.... 'hello     how       are       you'.  I am not able to do this as everytime it is concatenating same repeated values into string and I need this concatenate here inside loop only.

Please let me know if somebody came accross similar issue before.

Thanks,

Sanjana

12 REPLIES 12
Read only

Former Member
0 Likes
2,979

SO show us the code that you are using and perhaps we will see what is amiss.

Neal

Read only

Former Member
0 Likes
2,979

please post code and expected output.

BR.

Read only

Former Member
0 Likes
2,979

Hi Sanjana,

I didn't get what actually is w_string. Is it string or string table?

If its just concatenate table values into a string,inside a loop, you can use below sample code.

LOOP AT lt_string INTO ls_string.

    CONCATENATE string ls_string INTO string SEPARATED BY space.

    CLEAR ls_string.

ENDLOOP.

If you are looking for something else, please post your code so that the requirement could be better understood and if there is anything missing can be looked into.

Shanmukh Srinivas

Read only

Former Member
0 Likes
2,979

your requirement is not clear, why are doing the same thing in two different string variables? what are you trying to achive with this. Also paste your code so that we can see what is going wrong.

regards

swanand

Read only

arindam_m
Active Contributor
0 Likes
2,979

Hi,

The code can be like:

clear: w_string.

loop on it_line.

   CONCATENATE w_string it_line-v_str into w_string SEPARATED BY space.

   CONDENSE w_string.

endlloop.

In 1st iteration

Before concatenation:

w_string = ' '

it_line-v_str = Hello

After concatenation:

w_string = ' Hello'

After condense:

w_string = 'Hello' (leading space removed)

In 2nd iteration

Before concatenation:

w_string = 'Hello'  (Value after 1st iteration)

it_line-v_str = How

After concatenation:

w_string = ' Hello How'

After condense:

w_string = 'Hello How' (leading space and redundant spaces in between if any removed)

and so on...

Assuming that's what you asked for..

Cheers,

Arindam

Read only

former_member186413
Participant
0 Likes
2,979

Hi,  sanjana lingras

If I were you I would write the code like this.
************suppose the itab type is declear as
DATA: itab TYPE TABLE OF STRING,
           w_string  TYPE STRING,
           w_string1 TYPE STRING.
*******From this point you fill the itab with you strings. 

LOOP AT itab INTO wa_string.

           CONCATENATE itab-ztext wa_string INTO itab-ztext SEPERATE BY SPACE.

ENDLOOP.

w_string1 = w_string.
CONDENCE w_string1 NO-GAPS.
NOTE: APPEND ONLY USED FOR ITAB (DATASET) OR  DATABASE MODIFICATION .
           Please VIEW this link about processing string http://wiki.sdn.sap.com/wiki/display/WDABAP/STRING+FUNCTIONS
Read only

anilkumar_kalkivai
Active Participant
0 Likes
2,979

Hi,

The APPEND statement used to append the records not for CONCATENATION.

You have to use CONCATENATE statement inside the loop to get string string values concatenated.

The code part given by Arindam Mondal is the correct one.

Regards,

Anil.

Read only

UmaArjunan
Active Participant
0 Likes
2,979

Hi Sanjana,

For your requirement , to get the unique string inside the loop. you can try like this.

TYPES : BEGIN OF ty_line,

           text TYPE char10,

          END OF ty_line.

  DATA : it_line TYPE STANDARD TABLE OF ty_line,

             lw_line LIKE LINE OF it_line.

  DATA : w_string TYPE string.

  lw_line-text = 'hello'      .

  APPEND lw_line TO it_line.

  lw_line-text = 'how'      .

  APPEND lw_line TO it_line.

  lw_line-text = 'are'      .

  APPEND lw_line TO it_line.

  lw_line-text = 'how'      .

  APPEND lw_line TO it_line.

  lw_line-text = 'you'      .

  APPEND lw_line TO it_line.

  lw_line-text = 'hello'      .

  APPEND lw_line TO it_line.

  LOOP AT it_line INTO lw_line .

    FIND lw_line-text IN w_string.      " If same word is NOT found , then combine it

    IF sy-subrc NE 0.

      CONCATENATE w_string lw_line-text  INTO w_string SEPARATED BY space.

    ENDIF.

    endloop.

    WRITE : / 'String without repeated words', w_string.

Read only

Former Member
0 Likes
2,979

Go to abap help documentation and understand the functionality of append and concatenate statements.

Read only

former_member209120
Active Contributor
0 Likes
2,979

Hi Sanjana,

Try like this..

TYPES : BEGIN OF ty_itab,
         line1(20) TYPE c,
         END OF ty_itab.

DATA : it_itab TYPE TABLE OF ty_itab,
        wa_itab type ty_itab,
        w_var type string.

wa_itab-line1 = 'hello'.
APPEND wa_itab TO it_itab.

wa_itab-line1 = 'how'.
APPEND wa_itab TO it_itab.

wa_itab-line1 = 'are'.
APPEND wa_itab TO it_itab.

wa_itab-line1 = 'you'.
APPEND wa_itab TO it_itab.

clear : wa_itab, w_var.
LOOP AT it_itab INTO wa_itab.
       CONCATENATE w_var wa_itab-line1  INTO w_var SEPARATED BY space.
ENDLOOP.

WRITE : / w_var.

Output:


TYPES : BEGIN OF ty_itab,
         line1 TYPE string,
         END OF ty_itab.

TYPES : BEGIN OF ty_itab1,
         sno(4) type c,
         line1 TYPE string,
         END OF ty_itab1.

DATA : it_itab TYPE TABLE OF ty_itab,
        wa_itab type ty_itab,
        w_var type string.

data : it_itab1 TYPE TABLE OF ty_itab1,
        wa_itab1 type ty_itab1.

do 10 times.
wa_itab1-sno = wa_itab1-sno + 1.
append wa_itab1 to it_itab1.
enddo.

wa_itab-line1 = 'hello'.
APPEND wa_itab TO it_itab.

wa_itab-line1 = 'how'.
APPEND wa_itab TO it_itab.

wa_itab-line1 = 'are'.
APPEND wa_itab TO it_itab.

wa_itab-line1 = 'you'.
APPEND wa_itab TO it_itab.

clear : wa_itab, w_var.

LOOP AT it_itab INTO wa_itab.
       CONCATENATE w_var wa_itab-line1 INTO w_var SEPARATED BY space.
ENDLOOP.

Loop at it_itab1 into wa_itab1.
wa_itab1-line1 = w_var.
modify it_itab1 from wa_itab1 TRANSPORTING line1.
clear wa_itab1.
endloop.

Loop at it_itab1 into wa_itab1.
WRITE : / wa_itab1-sno, wa_itab1-line1.
endloop.

Output:



Read only

former_member194152
Contributor
0 Likes
2,979

Try this code :

REPORT YTEST.
data gt_tab type table of string.
data gs_tab type string.
data result type string.
data length type i.
gs_tab = 'Hello'. append gs_tab to gt_tab.
gs_tab = 'Hello How'. append gs_tab to gt_tab.
gs_tab = 'Hello How Are'. append gs_tab to gt_tab.
gs_tab = 'Hello How Are You'. append gs_tab to gt_tab.

loop at gt_tab into gs_tab.
  if result is INITIAL.
    result = gs_tab.
  else.
    length = strlen( result ).
    CONCATENATE result gs_tab+length into result.
  endif.
  clear gs_tab.
endloop.
write : / result.

Regards,

Gagan

Read only

sanjana_lingras
Active Participant
0 Likes
2,979

Hi All,

Thanks for the quick help, this is no more required. This is solved using offset and length.

thanks,

Sanjana