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: 

TEXT_SYMBOL_REPLACE for variables longer than 132 char?

joan_ayala
Participant

Hi all,

I am replacing variables in standard texts correctly using FMs INIT_TEXTSYMBOL, GET_TEXTSYMBOL, SET_TEXTSYMBOL and TEXT_SYMBOL_REPLACE.


The problem is that when I use TEXT_SYMBOL_REPLACE, the content of my variable is longer than 132 char (used in tables parameter 'LINES'), so the output results in my variable being truncated.

Anybody could help me? Thank you very much

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

I think you can't use symbols of more than 132 characters. A workaround could be defining several symbols in the text (pay attention to not split words).

4 REPLIES 4

Sandra_Rossi
Active Contributor

I think you can't use symbols of more than 132 characters. A workaround could be defining several symbols in the text (pay attention to not split words).

0 Kudos

Thank you Sandra, but I need to use symbols of more than 132 characters, and I don't want to define more than one symbol...

0 Kudos

In fact, I think the limit is 131 characters, not 132 (tested in 7.31). Anyway, one generic solution is to replace repeatedly with 131 characters at a time, then add a symbol at the end of these 131 characters for the rest of characters, repeat again, until all characters of the symbol appear in the SAPscript text (kind of recursive).

DATA longtext(2000) TYPE c.
DATA ls_header TYPE thead.
DATA ls_line TYPE tline.
DATA lt_line TYPE TABLE OF tline.

ls_header-tdspras = sy-langu.
ls_header-tdlinesize = 72.
ls_line-tdformat = '/'.
ls_line-tdline = 'START&LONGTEXT&END'.
APPEND ls_line TO lt_line.

CONCATENATE
'Lorem ipsum dolor sit amet, consectetur adipiscing '
'elit, sed do eiusmod tempor incididunt ut labore et '
'dolore magna aliqua. Ut enim ad minim veniam, quis '
'nostrud exercitation ullamco laboris nisi ut aliquip ex '
'ea commodo consequat. Duis aute irure dolor in '
'reprehenderit in voluptate velit esse cillum dolore eu '
'fugiat nulla pariatur. Excepteur sint occaecat '
'cupidatat non proident, sunt in culpa qui officia '
'deserunt mollit anim id est laborum.'
INTO longtext RESPECTING BLANKS.

PERFORM text_symbol_replace USING ls_header CHANGING lt_line.

LOOP AT lt_line INTO ls_line.
  WRITE : / ls_line-tdline.
ENDLOOP.

*&---------------------------------------------------------------------*
*&      Form  text_symbol_replace
*&---------------------------------------------------------------------*
FORM text_symbol_replace
      USING
        is_header TYPE thead
      CHANGING
        ct_line   TYPE tline_tab.
  TYPES : BEGIN OF ty_ls_longsymbol,
            name            TYPE itcst-name,
            itfname         TYPE itcst-name, "symbol name in form &SYMBOL&
            tempname        TYPE itcst-name, "symbol name in form ^SYMBOL$
            symendoff       TYPE i, " highest offset at end of "max_text" where symbol can be located
            original_value  TYPE string,
            work_value      TYPE string,
            ref_value       TYPE REF TO data,
            fully_replaced  TYPE abap_bool,
            pos             TYPE i,
          END OF ty_ls_longsymbol.
  CONSTANTS:
        max_len           TYPE i VALUE 131.
  DATA: lt_symbol         TYPE TABLE OF itcst,
        max_text(max_len) TYPE c,
        ls_longsymbol     TYPE ty_ls_longsymbol,
        lt_longsymbol     TYPE TABLE OF ty_ls_longsymbol,
        type              TYPE abap_typekind.
  FIELD-SYMBOLS:
        <ls_longsymbol>   TYPE ty_ls_longsymbol,
        <ls_symbol>       TYPE itcst,
        <field>           TYPE simple.
  " Get the list of symbol names contained in the SAPscript text
  CALL FUNCTION 'TEXT_SYMBOL_COLLECT'
    TABLES
      lines   = ct_line
      symbols = lt_symbol.
  " Get those symbols whose values are C or STRING with value longer
  " than 131 (if any) and store them for later special processing
      " NOTE : there's a maximum of 131 characters because for some
      " reason TEXT_SYMBOL_REPLACE ignores the 132th character (731).
  LOOP AT lt_symbol ASSIGNING <ls_symbol>.
    ASSIGN (<ls_symbol>-name) TO <field>.
    IF sy-subrc = 0.
      DESCRIBE FIELD <field> TYPE type.
      IF type CA 'Cg' AND strlen( <field> ) > max_len.
        ls_longsymbol-name = <ls_symbol>-name.
        CONCATENATE '&' <ls_symbol>-name '&' INTO ls_longsymbol-itfname.
        CONCATENATE '^' <ls_symbol>-name '$' INTO ls_longsymbol-tempname.
        ls_longsymbol-symendoff = max_len - strlen( ls_longsymbol-itfname ).
        ls_longsymbol-original_value = <field>.
        ls_longsymbol-work_value = <field>.
        ls_longsymbol-fully_replaced = abap_false.
        APPEND ls_longsymbol TO lt_longsymbol.
      ENDIF.
    ENDIF.
  ENDLOOP.

  DO.
    LOOP AT lt_longsymbol ASSIGNING <ls_longsymbol> WHERE fully_replaced = abap_false.
      max_text = <ls_longsymbol>-work_value.
      IF strlen( <ls_longsymbol>-work_value ) > max_len.
        " regex to get last word:
        "
        " if the LONGTEXT symbol/variable is 143 characters as follows (from position 106) :
        "
        " 6789 110 456789 120 456789 130 456789 140
        " sed do eiusmod tempor incididunt ut labore
        "
        " it will be replaced with a text containing the pseudo symbol which must not be longer than 131 characters :
        "
        " 6789 110 456789 120 456789 130
        " sed do eiusmod ^LONGTEXT$
        FIND REGEX ' [^ ]+$' IN max_text(<ls_longsymbol>-symendoff) MATCH OFFSET <ls_longsymbol>-pos.
        IF sy-subrc = 0.
          ADD 1 TO <ls_longsymbol>-pos.
        ELSE.
          <ls_longsymbol>-pos = <ls_longsymbol>-symendoff.
        ENDIF.
        max_text+<ls_longsymbol>-pos = <ls_longsymbol>-tempname.
      ELSE.
        <ls_longsymbol>-fully_replaced = abap_true.
      ENDIF.
      ASSIGN <ls_longsymbol>-ref_value->* TO <field>.
      <field> = max_text.
    ENDLOOP.
    CALL FUNCTION 'TEXT_SYMBOL_REPLACE'
      EXPORTING
        header = is_header
      TABLES
        lines  = ct_line.
    LOOP AT lt_longsymbol ASSIGNING <ls_longsymbol> WHERE fully_replaced = abap_false.
      REPLACE ALL OCCURRENCES OF <ls_longsymbol>-tempname IN TABLE ct_line WITH <ls_longsymbol>-itfname.
      <ls_longsymbol>-work_value = <ls_longsymbol>-work_value+<ls_longsymbol>-pos.
    ENDLOOP.
    IF sy-subrc <> 0.
      " all symbols are fully replaced
      EXIT.
    ENDIF.
  ENDDO.
  " restore original symbol values
  LOOP AT lt_longsymbol ASSIGNING <ls_longsymbol>.
    ASSIGN <ls_longsymbol>-ref_value->* TO <field>.
    <field> = <ls_longsymbol>-original_value.
  ENDLOOP.
ENDFORM.                    "text_symbol_replace 

0 Kudos

I forgot to answer (4 years ago) that the code works for one symbol of more than 131/132 characters. In the example, it must be approximately 500 characters.