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

Splitting a string into multiple parts from particular position (string has no delimiter )

Former Member
0 Likes
5,257

Hello Gurus,

I need to make string of having length 1000 into multiple parts from a particular position which has some text without any delimiter and i want to populate each part into an internal table which has one filed...

eg : string = 'sap abap sap abap sap abap sap abap sap abap sap abap sap abap sap sap sap abap sap abap'

              

suppose i want to start splitting from 40th position...assume that i have 3 parts

and these 3 parts i have put into an internal table..

lt_itab = [sap abap sap abap sap abap sap abap,

              sap abap sap abap sap abap sap abap,

               sap abap sap sap sap abap sap abap]

plese help!

any help would be Appreciated ...thank you

1 ACCEPTED SOLUTION
Read only

dibyajeeban_jena
Active Participant
0 Likes
2,558

hi,

check below coding and use  fm - SWA_STRING_SPLIT ,,

* 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.

Regards DJ

Hello Gurus,

I need to make string of having length 1000 into multiple parts from a particular position which has some text without any delimiter and i want to populate each part into an internal table which has one filed...

eg : string = 'sap abap sap abap sap abap sap abap sap abap sap abap sap abap sap sap sap abap sap abap'

              

suppose i want to start splitting from 40th position...assume that i have 3 parts

and these 3 parts i have put into an internal table..

lt_itab = [sap abap sap abap sap abap sap abap,

              sap abap sap abap sap abap sap abap,

               sap abap sap sap sap abap sap abap]

plese help!

any help would be Appreciated ...thank you

10 REPLIES 10
Read only

Former Member
0 Likes
2,558

Hi Nagendra,


I'm just sharing code which need to changed accordling. Just an idea


DATA : w_str TYPE string VALUE 'abapaaaaaaaaaaaaaaaaaaaaaaaaa aabaaaaaaaaaaaaaaaaaaqqqqq',

           part  TYPE n VALUE 3,

          w_lengh(4) TYPE n,

          offset(3) TYPE n,

          start(3TYPE n,

         wa_area(100)   TYPE c .


* String lengh

w_lengh = strlen( w_str ).

offset = w_lengh  DIV part .

start = 0.

DO part times.

   wa_area = w_str+start(offset).

* APPEND wa_rea to t_table.

   start = start + offset.

ENDDO.


Thanks & Regards,

Arun

Read only

0 Likes
2,558

Thank you so much for your reply

Read only

mayur_priyan
Active Participant
0 Likes
2,558

Hi,

Try this code.

TYPES: BEGIN OF ty,

        b TYPE string,

        END OF ty,

        tt TYPE STANDARD TABLE OF ty.

DATA: a TYPE string VALUE 'aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll',

       tb TYPE tt,

       wa TYPE ty,

       off TYPE i,

       len TYPE i,

       count TYPE i,

       count1 TYPE i,

       temp TYPE i.

len = 8.                      "Position

off = 0.

count = strlen( a ).

count1 = count / 8.

temp = len - 1.

DO count1 times.

  temp = off + len.

  IF temp > count.

   wa-b = a+off.

  ELSE.

   wa-b = a+off(len).

  ENDIF.

  off = off + len.

  SHIFT wa-b LEFT DELETING LEADING space.

  APPEND wa to tb.

ENDDO.

LOOP AT  tb INTO wa.

write: / wa-b.

ENDLOOP.

Read only

dibyajeeban_jena
Active Participant
0 Likes
2,559

hi,

check below coding and use  fm - SWA_STRING_SPLIT ,,

* 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.

Regards DJ

Read only

0 Likes
2,558

Thank you...

Read only

Former Member
0 Likes
2,558

Here's one more way to do it.

  1. DATA str TYPE string VALUE 'first012345679801234567890123456789'.
  2. DATA lt TYPE TABLE OF char10.
  3. DO.
  4.   TRY .
  5.       APPEND str(10) TO lt.
  6.       str = str+10.
  7.     CATCH cx_sy_range_out_of_bounds.
  8.       APPEND str TO lt.
  9.       EXIT.
  10.   ENDTRY.
  11. ENDDO.

/.

Read only

mayur_priyan
Active Participant
0 Likes
2,558

I wonder why you have marked your 'Thank you' replies as Correct/Helpful Answers.

Read only

0 Likes
2,558

Hi,

Some time when I try to solve a problem I wake up the next day with the solution.

My theory is that I have "A SPLIT personality" and the other guy is much smarter then I am.....

Regards.

Read only

matt
Active Contributor
0 Likes
2,558

Fixed

Read only

0 Likes
2,558

Well that was a quick response. Thanks