Strings


  1. Replace and condense
  2. Concatenating
  3. Substrings and Formatted Write and Move
  4. Removing leading/trailing characters.
  5. Search for a string within a string
  6. Word wrapping/Split
  7. Splitting delimited string


Replace and condense


Replace replaces characters within a string. If a character is substituted with a space, you can use Condense to remove the space.

REPLACE '.' WITH ',' INTO w_tmpstr

Example:

 w_matnr = '3617203-100'
 REPLACE '-' WITH '' INTO w_matnr.                           
 CONDENSE w_matnr NO-GAPS.                                   

w_matnr will now contain: 3617203100

                                   

Concatenating


  CONCATENATE f1 ... fn INTO g.    
                                   
  Addition:                        
                                   
  ... SEPARATED BY h               

Example:

   CONCATENATE 'Der er sket en fejl i opdateringen'
                ITAB-ID ITAB-HKONT ITAB-DMBTR INTO ERRTXT
                SEPARATED BY SPACE.

SPACE is an ABAP keyword.




Substrings and Formatted Write and Move


You can use either WRITE or MOVE.

Using WRITE the format of the target field will be the same as when you write to a list. That means that the settings in the user's master record for decimal point and date are taking into account.

If you use MOVE, the decimal point is always a periode.

Syntax:


move t1+offset(length) to t2

write t1+offset(length) to t2

Example:

data: t1(10) type c, t2(10) type c.

t1 = '0123456789'.

move t1+3(5) to t2.

t2 will contain 34567

Instead of move you could have used write. In this example the result would have been the same.


Removing leading/trailing characters.


SHIFT myvar left deleting leading '0'.
SHIFT myvar right deleting trailing '0

Search for a string within a string


String1 = 'This is a string'.
SEARCH String1 for 'is'.

If the search string is found SY-SUBRC = 0
SY-FDPOS = 5 because the strinmg "is" starts at offset 5.


You can also use the Relational Operators for Character-Like Fields
CO, CN, CA, NA .... see the ABAP documentation for further informat


Word wrapping/Split


You can use the fucntion module SWA_STRING_SPLIT to split a tring into
smaller strings or use function module SWA_STRING_SPLIT

-----

>Does someone know how to word wrap fields from one internal table to
>a field in another internal table, when the fields have a different
>lengths ?

>E.g.

>Table 1 has a field with a line length of 50 characters, and I want
>to place each line in table one in one or more lines in table 2 that
>has a field length of 10: 

 
>table1-field1 = 'This is a very long line that should be wrapped'

>should be

>table2-Field1:
>sy-tabix = 1:  This is a

>sy-tabix = 2:  very long
>sy-tabix = 3:  line that
>sy-tabix = 4:  should be
>sy-tabix = 5:  wrapped


This example shows how to split the text in a field of one internal
table and place it into a field in another internal table that has
a shorter size


REPORT z_hfa_test .

DATA:
* Table containing texts that has to be split
  BEGIN OF it_longstrings OCCURS 0,
    line TYPE string,
  END OF it_longstrings,

* Output table from function SWA_STRING_SPLIT

  it_string_components LIKE swastrtab OCCURS 0 WITH HEADER LINE,

* Table containg the resukting strings
  BEGIN OF it_shortstrings OCCURS 0,
    line(10) TYPE c,
  END OF it_shortstrings,


start-of-selection.
* Add some lines to it_longstrings
it_longstrings-line = '123456789012345 Hallo this is a very long'.
APPEND it_longstrings.
it_longstrings-line = 'line, that has to be split.'.
APPEND it_longstrings.
  it_longstrings-line = 'The line has to be split into lines of 10

  characters'.
APPEND it_longstrings.


* Split each line of it_longstrings into lines with length 10
* The resuklting lines are palced in table it_string_components
LOOP AT it_longstrings.
  CLEAR it_string_components. REFRESH it_string_components.

  CALL FUNCTION 'SWA_STRING_SPLIT'
    EXPORTING
      input_string                       = it_longstrings-line
      max_component_length               = 10
*     TERMINATING_SEPARATORS             =
*     OPENING_SEPARATORS                 =

    TABLES
      string_components                  = it_string_components

*   EXCEPTIONS
*     MAX_COMPONENT_LENGTH_INVALID       = 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.

* Add the lines to table
  LOOP AT it_string_components.
    it_shortstrings-line = it_string_components-str.
    APPEND it_shortstrings.

  ENDLOOP.



ENDLOOP.


END-OF-SELECTION.
  LOOP AT  it_shortstrings.
    WRITE: / it_shortstrings-line.

  ENDLOOP.


7. Using CA -  Contains any (CO, CN,  NA)

  belob = '300120.00'.



   if belob CA '.'.
    l_pos = sy-fdpos.

    move  mystring+l_pos to mydec.
    if mydec CO '.0'.
       move mystring+0(l_pos) to mystring2.
    endif.
  endif.


Splitting delimited string


Instead of placing the substring ind l_output, it could be palced in an

internal
table using this syntax:

SPLIT f AT g INTO TABLE itab.

Each substring will be placed in its own table row. In the example below, the table would have 5 rows.
Note that all output fields must be of type char or string


REPORT YDK8HENFR_TEST1 line-size 80.


data: l_string(132) type c.

data: begin of l_output,
       name(40)     type c,
       age(10)      type c,
       address(40)  type c,
       city(40)     type c,
       zipcode(4)   type c,

     end of l_output.


start-of-selection.

  l_string = 'John;28;My street;My city;3000'.


  split l_string at ';' into l_output-name
                             l_output-age
                             l_output-address
                             l_output-city
                             l_output-zipcode.

  write: / l_output-name.
  write: / l_output-age.
  write: / l_output-address.
  write: / l_output-city.
  write: / l_output-zipc

 

Converted from CHM to HTML with chm2web Standard 2.7 (unicode)