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

How to split a string with a space in between?

Former Member
0 Likes
16,985

Hi ABAP Guru's,

Can you please help me out? How can I split a string into two which has a space in between?

For example: lets suppose the string is 'LA CA USA'. How can I split this string? I have to dynamically determine the number of characters before the space therefore I cannot use number constant such as 2 or 3.

With best regards,

Ketan

8 REPLIES 8
Read only

amit_khare
Active Contributor
0 Likes
6,679

SPLIT <ITAB/STRING> AT ' ' into table ITAB1.

Regards,

Amit

reward all helpful replies.

Read only

Former Member
0 Likes
6,679

Hi Ketan,

use 'Replace' space with '$'

It will fill up all spaces with $

and then split at '$'

Reward if useful!

Read only

Former Member
0 Likes
6,679
try this..

REPORT ychatest.

DATA : v_str TYPE string VALUE 'LA CSD KDGKL'.

DATA : BEGIN OF itab OCCURS 0,
         fld(250),
       END OF itab.

SPLIT v_str AT space INTO TABLE itab.

LOOP AT itab.
  WRITE : / itab-fld.
ENDLOOP.
Read only

Shahid
Product and Topic Expert
Product and Topic Expert
6,679

split v_name at space into v_a v_b v_c .

Read only

Former Member
0 Likes
6,679

data: itab type table of string.

split your_string at space into itab.

reward points if helpful

Read only

Former Member
0 Likes
6,679

hi,

To split a character string into two or more smaller strings, use the SPLIT statement as follows:

<b>SPLIT <c> AT <del> INTO <c1> ... <cn>.</b>

The system searches the field <c> for the separator <del>. The parts before and after the separator are placed in the target fields <c1> ... <cn>.

To place all fragments in different target fields, you must specify enough target fields. Otherwise, the last target field is filled with the rest of the field <c> and still contains delimiters.

If all target fields are long enough and no fragment has to be truncated, SY-SUBRC is set to 0. Otherwise it is set to 4.

<b>SPLIT LA CA USA AT ' ' INTO <c1></b>

regards,

Sudheer

Read only

6,679

Hi

Try like this

DATA : opbal TYPE p DECIMALS 2 VALUE 0.

    SELECT * FROM zs_mkpf_mseg
                    into corresponding fields of table it_opbal
                    WHERE werks =  itmat-werks
                    AND matnr = itmat-matnr
                    AND budat = s_budat-low.


  *  sort  it_opbal by budat.
  loop at it_opbal.
read table it_opbal. 
      IF it_opbal-shkzg = 'S'.
        opbal = opbal + it_opbal-menge.
      ELSEIF it_opbal-shkzg = 'H'.
        opbal = opbal + it_opbal-menge * -1.
      ENDIF.
  endloop.
    append opbal TO itab-opbal.

  ENDFORM.                    " opbal_data

you can also use thi s

DATA: NAMES(30)    TYPE C VALUE 'Charly, John , Peter', 
      NAMES2       TYPE STRING, 
      ONE(10)      TYPE C, 
      TWO(10)      TYPE C, 
      THREE        TYPE STRING, 
      FOUR(4)      TYPE C VALUE 'FOUR', 
      DELIMITER(2) VALUE ','. 

SPLIT NAMES AT DELIMITER INTO ONE TWO. 
*     ONE contains 'Charly' and TWO contains 'John , Pet'. 
*     SY-SUBRC is 4, because TWO was not large enough to 
*     accommodate the whole of the remaining string 
SPLIT NAMES AT ',' INTO ONE TWO THREE. 
*     ONE contains 'Charly', TWO contains ' John', 
*     THREE contains ' Peter'. 
SPLIT NAMES AT ', ' INTO ONE THREE TWO. 
*     ONE contains 'Charly', THREE contains 'John', 
*     TWO contains 'Peter'. 
CONCATENATE NAMES '' INTO NAMES2 SEPARATED BY SPACE. 
SPLIT NAMES2 AT DELIMITER INTO ONE TWO THREE FOUR. 
*     ONE contains 'Charly', TWO contains 'John', 
*     THREE contains 'Peter ', FOUR is empty. 
SPLIT NAMES2 AT DELIMITER INTO ONE FOUR THREE. 
*     ONE contains 'Charly', FOUR contains 'John', 
*     THREE contains 'Peter', SY-SUBRC is 4, since 
*     FOUR was not large enough (spaces are significant 
*     characters!) 

Reward all helpfull answers

Regards

Pavan

Read only

Former Member
0 Likes
6,679

Thanks a lot for your replies.

How can I use the search functionality here to determine the length of the first word before the space?

For example lets suppose the string is 'Uzbekistan Russia USA' how can I use the search or any other functionality to determine the length of Uzbekistan? I cannot use a constant number and I have to dynamically calculate for each string.

With best regards,

Ketan