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

Converting Internal table contents to string

Former Member
0 Kudos
24,854

Hi All,

I am trying to convert each row of internal table into string using the function module SAP_CONVERT_TO_TXT_FORMAT . Using this I can able to convert into string. But if one of the field is blank, it is concatenating the field and not showing the space for that field. In that place, the next field value is coming.

I want to pass the internal table line items to the string, without concetanating the field values.

Can anyone guide me.

Thanks in advance.

Regards

Ramesh.

1 ACCEPTED SOLUTION
Read only

adam_krawczyk1
Contributor
10,445

My proposal:

CONCATENATE LINES OF lt_string_table INTO l_single_string SEPARATED BY cl_abap_char_utilities=>cr_lf.
8 REPLIES 8
Read only

Former Member
0 Kudos
10,443

Why don´t you just copy the content of the whole workarea to a string linewise?

Read only

0 Kudos
10,443

Hi,

I have 45 fields in my internal table. It is Laborious process to do that.

Read only

0 Kudos
10,443

Hi,

Thanks for your suggestion. It is resolved.

Regards

Ramesh.

Read only

Former Member
0 Kudos
10,443

How was it solved?

For people who are interested, I had the same issue, I finally succeeded with the following code (it may be enhanced by adding a length parameter so that it can be used after cl_gui_frontend_services=>gui_upload to convert text table into string:

REPORT  z_merge_itab_respecting_blanks.

DATA it TYPE TABLE OF char20 WITH HEADER LINE.
DATA v TYPE string.

APPEND 'xxxj' TO it.
APPEND 'jj' TO it.
PERFORM table_2_string USING it[] CHANGING v.
WRITE v. "displays xxxj                jj

FORM table_2_string USING it TYPE ANY TABLE CHANGING v TYPE string.
  FIELD-SYMBOLS <z> TYPE ANY.
  DATA str TYPE string.
  DATA len TYPE i.
  DATA j TYPE i.
  DATA blanks TYPE string.

  CLEAR v.
  LOOP AT it ASSIGNING <z>.
    EXIT.
  ENDLOOP.
  IF sy-subrc EQ 0.

    DESCRIBE FIELD <z> LENGTH len IN CHARACTER MODE.
    SHIFT blanks RIGHT BY len PLACES.

    LOOP AT it ASSIGNING <z>.
      str = <z>.
      j = len - STRLEN( str ).
      IF j EQ 0.
        CONCATENATE v str INTO v.
      ELSE.
        CONCATENATE v str blanks(j) INTO v.
      ENDIF.
    ENDLOOP.

  ENDIF.

ENDFORM.                    "table_2_string

Read only

0 Kudos
10,443

Instead of coding yourself the conversion, you can use function module SWA_STRING_FROM_TABLE (available in any sap install) :

* The following converts an internal table into string
* Demo : internal table with 2 lines of 5 characters each:
* ' xxx ' and ' hhh ' is converted into
* ' xxx  hhh ' (10 characters long, with one trailing space)
REPORT  zz_table_to_string.

DATA itab1 TYPE TABLE OF char5.
DATA str TYPE string.

APPEND ' xxx ' TO itab1.
APPEND ' hhh ' TO itab1.

CALL FUNCTION 'SWA_STRING_FROM_TABLE'
  EXPORTING
    character_table            = itab1
*   NUMBER_OF_CHARACTERS             =
*   LINE_SIZE                        =
    keep_trailing_spaces       = 'X'
*   CHECK_TABLE_TYPE                 = ' '
  IMPORTING
    character_string           = str
  EXCEPTIONS
    no_flat_charlike_structure = 1
    OTHERS                     = 2.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

data i type i.
i = strlen( str ).
assert i = 10.

data test(10) type c.
test = str.
assert test = ' xxx  hhh'.

write : / str, i.

Read only

0 Kudos
10,443

How can I replace the space between two string with a comma for the example shown above?

I am expecting for an output like:
xxx, hhh

Read only

0 Kudos
10,443

Hi,

If you are planning to create CSV file I suggest you follow the rules of CSV .

Comma-separated values - Wikipedia, the free encyclopedia

Regards.

Read only

adam_krawczyk1
Contributor
10,446

My proposal:

CONCATENATE LINES OF lt_string_table INTO l_single_string SEPARATED BY cl_abap_char_utilities=>cr_lf.