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: 

How to write ABAP code to split a sentence into Word by Word and store them

Former Member
0 Kudos

Hi all,

I have to split a sentence into word by word and store them into separate columns. This sentence is nothing but a text description (TXTMD) from table TXXXX. The description will have 3 or more than 3 words and LAST word will be <b>always with %</b>'. The following is the sample input data:

KEY(ORD43) Description(TXTMD)

'AAAA' '1234 ABCD COMPANY USA 80%'

'BBBB' '4745 XYZ INC CANADA ABCD 70%'

For the first record:

'1234' should stored in to <b>Field 1</b>, 'ABCD COMPANY USA' into <b>Field 2</b> and

'80%' into <b>Filed 3</b>.

For the second record:

'4745' should stored in to <b>Field 1</b>, 'XYZ INC CANADA ABCD' into <b>Field 2</b> and 70%' into <b>Field 3</b>.

The first word into Field 1, the last word with % into Field 3 and all middle words into Field 2. The number of words in the sentence could be 3 or more than 3.

Could you please help in writing the ABAP for this requirement ????

Thanks in advance.

Regards,

Venkat.

15 REPLIES 15

Former Member
0 Kudos

Use the SPLIT command.

Rob

0 Kudos

Hi Rob,

The first word into Field 1, the last word with % into Field 3 and all middle words into Field 2. The number of words in the sentence could be 3 or more than 3.

Thanks,

Vena.

0 Kudos

It was just meant to get you going. You can easily add the logic to re-combine and get the results you need.

Rob

0 Kudos

hi ...

define an internal table..

<b>data :begin of itab occurs 0,

field(15),

end of itab.

data w_line type i.

split TXMTD at space into table itab.

describe table itab lines w_line.

loop at itab.

if sy-tabix eq 1.

var1 = itab-field.

continue.

endif.

if sy-tabix eq w_line.

var3 = itab-field.

continue.

endif.

concatenate var2 itab-field into var2 separated by space.

endloop.</b>

Please do remember to close the thread when ur problem is solved !!!

Regards,

Sai Ramesh

Former Member
0 Kudos

<b>split txtmd at space into variable1 variable2.</b>.........

Regards,

Sai Ramesh

Former Member
0 Kudos

Try something like this.

DATA: BEGIN OF itab OCCURS 0,
        key(4),
        descr(40).
DATA: END OF itab.

DATA: BEGIN OF itab_words OCCURS 0,
        word(20).
DATA: END OF itab_words.

DATA: BEGIN OF itab_final OCCURS 0,
        field1(4),
        field2(30),
        field3(4).
DATA: END OF itab_final.

itab-key   = 'AAAA'.
itab-descr =  '1234 ABCD COMPANY USA 80%'.
APPEND itab.
CLEAR itab.

itab-key   = 'BBBB'.
itab-descr = '4745 XYZ INC CANADA ABCD 70%'.
APPEND itab.
CLEAR itab.

LOOP AT itab.
  SPLIT itab-descr AT space INTO TABLE itab_words.
  DO.
    READ TABLE itab_words INDEX sy-index.
    IF sy-subrc <> 0.
      EXIT.
    ENDIF.
    IF sy-index = 1.
      itab_final-field1 = itab_words-word.
      CONTINUE.
    ENDIF.
    IF itab_words-word CA '%'.
      itab_final-field3 = itab_words-word.
      APPEND itab_final.
      CLEAR itab_final.
    ELSE.
      CONCATENATE itab_final-field2
                  itab_words-word
             INTO itab_final-field2 SEPARATED BY space.
    ENDIF.
  ENDDO.
ENDLOOP.

LOOP AT itab_final.
  WRITE:/ itab_final-field1,
          itab_final-field2,
          itab_final-field3.
ENDLOOP.

0 Kudos

Hi Srinivas,

Thank you very much for the detailed response.

Sorry, for not giving the complete details.

It has to read the table TXXXX in a loop with Key Field KEYFIG and split the words for each record. I can not hardcode in the program.

Thanks again.

Regards,

Venkat.

0 Kudos

I hardcoded the records only as an example, but you can fill the same itab with records from a database table and then do the rest of it.

Former Member
0 Kudos

Hi,

You the below code :

report ztest.

data : begin of itab1 occurs 0,
        txt100(100) type c,
       end of itab1.


data : begin of itab occurs 0,
        bukrs like bsis-bukrs,
        txt50 like skaT-txt50,
        per(4) type c,
      end of itab.

DATA : LINE TYPE I,
      LEN TYPE I,
      TEMP TYPE I,
      TEMP1 TYPE I.


ITAB1-txt100 =  '1234 ABCD COMPANY USA 80%'.
APPEND ITAB1.
CLEAR ITAB1.

ITAB1-txt100 = '4745 XYZ INC CANADA ABCD 70%'.
APPEND ITAB1.
CLEAR ITAB1.


LOOP AT ITAB1.

  CALL FUNCTION 'STRING_LENGTH'
    EXPORTING
      string       = ITAB1-txt100
   IMPORTING
     LENGTH        = LEN.

TEMP = len.

TEMP = LEN - 7.

ITAB-BUKRS = ITAB1-TXT100.
ITAB-TXT50 = ITAB1-TXT100+4(TEMP).

TEMP1 = TEMP + 4.
ITAB-PER = ITAB1-TXT100+TEMP1(3).

APPEND ITAB.
CLEAR ITAB.

ENDLOOP

loop at itab.
write : /  ------
endloop.

Thanks,

Sriram Ponna.

Former Member
0 Kudos

REPORT ztest1111

NO STANDARD PAGE HEADING LINE-SIZE 255.

PARAMETERS: x TYPE string.

DATA: y TYPE string,

f1 TYPE string,

f2 TYPE string,

f3 TYPE string,

temp TYPE i,

temp1 TYPE i.

MOVE x TO y.

SPLIT y AT space INTO f1 f2.

temp = STRLEN( f2 ).

temp1 = temp - 3.

f3 = f2+temp1(3).

WRITE: /'field1',f1.

WRITE: /'field2',f2.

WRITE: /'field3',f3.

Former Member
0 Kudos

Hi Venkat,

This is a simple approach assuming the field length of the last field is fixed to 3.

PARAMETERS: x TYPE string.

DATA: y TYPE string,

f1 TYPE string,

f2 TYPE string,

f3 TYPE string,

temp TYPE i,

temp1 TYPE i.

MOVE x TO y.

SPLIT y AT space INTO f1 f2.

temp = STRLEN( f2 ).

temp1 = temp - 3.

f3 = f2+temp1(3).

WRITE: /'field1',f1.

WRITE: /'field2',f2.

WRITE: /'field3',f3.

former_member195698
Active Contributor
0 Kudos

Another way to perform your task....

wf_text = '1234 ABCD COMPANY USA 80%'.

split wf_text into wf_field1 wf_temp at ' '. (This is put the first word into field1 and rest in wf_temp)

Call the function STRING_REVERSE for wf_temp.

split the wf_temp into wf_field3 wf_field2 at ' '. (This will put reversed text into field2 and field3)

Call the Function STRING_REVERSE for wf_field2 and wf_field3.

Thanks & Regards,

Abhishek

Former Member
0 Kudos

Easy way if you know the length of the field.

In my example I assumed my field has 12 places.

DATA: obs TYPE string,

       bkp TYPE char12.

obs = '12345678901234567890123456789012345678901234567890'.

DO.

   MOVE obs TO bkp.

   SHIFT obs BY 12 PLACES LEFT.

   write: / bkp.   "Do what you want with bkp variable

   IF obs IS INITIAL.

     EXIT.

   ENDIF.

ENDDO.

0 Kudos

Celito its a 6 year old thread:)

0 Kudos

I know, but I needed the solution, didn't found.

This topic is the first result from Google, so others may need too.