Friday, August 2, 2013

CL_ABAP_CHAR_UTILITIES class use

The components of this class are utilities for the processing of characters. The components of this class are all static and public.When you are working with different file type( specially  Excel , text ) in SAP and you need to insert any special char like line feed , horizontal tab , vertical tab, page break etc. you can check these chars in class CL_ABAP_CHAR_UTILITIES in attributes.

CL_ABAP_CHAR_UTILITIES->HORIZONTAL_TAB attribute:

Tab character as delimiter in ABAP string.

REPORT ZTEST_PROGRAM.
DATA: BEGIN OF IT_FILE OCCURS 0,
        STR TYPE STRING,
      END OF IT_FILE.
"Tab delimited records
CONCATENATE 'Sachin'   'Dravid' 'Kumble' 'Ganguly'  INTO IT_FILE-STR SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
APPEND IT_FILE.CLEAR  IT_FILE.
CONCATENATE 'MS Dhoni' 'Kohli'  'Rohith' 'R.Jadeja' INTO IT_FILE-STR SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
APPEND IT_FILE.CLEAR  IT_FILE.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    FILENAME = 'C:\test01.txt'
  TABLES
    DATA_TAB = IT_FILE.
IF SY-SUBRC <> 0.
  WRITE 'Error while writing'.
ENDIF.

CL_ABAP_CHAR_UTILITIES->CR_LF attribute:

Carriage Return and Line Feed.

REPORT ZTEST_PROGRAM.
DATA: BEGIN OF IT_FILE OCCURS 0,
        STR TYPE STRING,
      END OF IT_FILE.
DATA:LV_STRING TYPE STRING.
"Lines separated by horizontal tab
CONCATENATE 'Sachin'   'Dravid' 'Kumble' 'Ganguly'  INTO IT_FILE-STR SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
APPEND IT_FILE.CLEAR  IT_FILE.
CONCATENATE 'MS Dhoni' 'Kohli'  'Rohith' 'R.Jadeja' INTO IT_FILE-STR SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
APPEND IT_FILE.CLEAR  IT_FILE.

"Internal tables records into single string variables using CR_LF attribute.
LOOP AT IT_FILE.
  CONCATENATE LV_STRING IT_FILE-STR CL_ABAP_CHAR_UTILITIES=>CR_LF INTO LV_STRING.
ENDLOOP.
DATA LT_STRING TYPE TABLE OF STRING.
APPEND LV_STRING TO LT_STRING.
"Download table
CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    FILENAME = 'C:\test02.txt'
  TABLES
    DATA_TAB = LT_STRING.
IF SY-SUBRC <> 0.
  WRITE 'Error while writing'.
ENDIF.

CL_ABAP_CHAR_UTILITIES->NEW_LINE attribute:

New line


REPORT ZTEST_PROGRAM.
DATA: BEGIN OF IT_FILE OCCURS 0,
        STR TYPE STRING,
      END OF IT_FILE.
DATA:LV_STRING TYPE STRING.
"Lines separated by horizontal tab
CONCATENATE 'Sachin'   'Dravid' 'Kumble' 'Ganguly'  INTO IT_FILE-STR SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
APPEND IT_FILE.CLEAR  IT_FILE.
CONCATENATE 'MS Dhoni' 'Kohli'  'Rohith' 'R.Jadeja' INTO IT_FILE-STR SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
APPEND IT_FILE.CLEAR  IT_FILE.

"converting string table to string
LOOP AT IT_FILE.
  IF SY-TABIX = 1.
    LV_STRING = IT_FILE-STR.
  ELSE.
    CONCATENATE LV_STRING IT_FILE-STR INTO LV_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>NEWLINE.
  ENDIF.
ENDLOOP.

DATA LT_STRING TYPE TABLE OF STRING.
APPEND LV_STRING TO LT_STRING.
"Download table
CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    FILENAME = 'C:\test03.txt'
  TABLES
    DATA_TAB = LT_STRING.
IF SY-SUBRC <> 0.
  WRITE 'Error while writing'.
ENDIF.

3 comments:


  1. Awesome post
    This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It?s the old what goes around comes around routine. Did you acquired lots of links and I see lots of trackbacks??
    Hadoop training

    ReplyDelete
  2. I would like to thank you for the efforts you have put in penning this site. I’m hoping to view the same high-grade content by you later on as well. In fact, your creative writing abilities has inspired me to get my own site now
    SAP APO Online Training

    ReplyDelete

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.