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: 

Splitting a string in different substrings

Former Member
0 Kudos
1,581

Hallo,

I want to split a string of any length in new substrings and want to save these substrings in different variables.

I tried as following:


data:
      string type string value 'Hallo SAP world',  "This could be any string with any lenght
      offset type i value 0,
      str_len type i,
      new_str type string.

str_len = strlen( string ).

do 5 times.
  new_str = string+offset(3).
  write : new_str.
  write : ' '.

  offset = offset + 3.
   enddo.

1_ Has anyone any idea, how to save the new substrings in different variables?

2_ Since the new substring has length 3. So how many times should the do-loop run?

Thanks

ama

1 ACCEPTED SOLUTION

faisal_altaf2
Active Contributor
0 Kudos
383

Hi, Muhammed Aftab Alam

Test the following Sample code it working the same way you are requiring, I have Tested,

REPORT zfsl_sub_string.

DATA: main_string TYPE string,
      len_string TYPE i,
      reminder TYPE i,
      string_from TYPE i.
main_string = 'Hallo SAP World'. " You can give any string here.
len_string = STRLEN( main_string ).

DO 3 TIMES.
  reminder = len_string MOD 3.
  IF reminder NE 0.
    CONCATENATE main_string ' ' into main_string SEPARATED BY space.
  len_string = strlen( main_string ).
  ENDIF.
ENDDO.
len_string = len_string div 3.

string_from = 0.
DO len_string TIMES.
  WRITE: / main_string+string_from(3).
  ADD 3 TO : string_from.
ENDDO.

Following too the same as above just add PARAMETER for input String

REPORT zfsl_sub_string.

PARAMETERS: pstring TYPE string.

DATA: "pstring TYPE string,
      len_string TYPE i,
      reminder TYPE i,
      string_from TYPE i.
*pstring = 'Hallo SAP World'. " You can give any string here.
len_string = STRLEN( pstring ).

DO 3 TIMES.
  reminder = len_string MOD 3.
  IF reminder NE 0.
    CONCATENATE pstring ' ' into pstring SEPARATED BY space.
  len_string = strlen( pstring ).
  ENDIF.
ENDDO.
len_string = len_string div 3.

string_from = 0.
DO len_string TIMES.
  WRITE: / pstring+string_from(3).
  ADD 3 TO : string_from.
ENDDO.

Please Reply if any Problem,

Kind Regards,

Faisal

Edited by: Faisal Altaf on Jan 27, 2009 3:06 PM

14 REPLIES 14

Former Member
0 Kudos
383

Hi,

Check this FM RKD_WORD_WRAP

Function module              RKD_WORD_WRAP

  Import parameters               Value

  TEXTLINE                        THIS IS THE STRING
  DELIMITER
  OUTPUTLEN                       3

  Export parameters               Value

  OUT_LINE1                       THI
  OUT_LINE2                       S
  OUT_LINE3                       IS

  Tables                          Value

  OUT_LINES                          0 Entries
                   Result:           7 Entries

0 Kudos
383

Hallo Avinash,

thanks for your suggestion.

How can I assign values to OUT_LINE?

Can this function be used for strings having more than 9 characters?


CALL FUNCTION 'RKD_WORD_WRAP'

  IMPORTING
    textline                  = string       "string = 'Hallo SAP world' or any other string
*   DELIMITER                 = ' '
   OUTPUTLEN                 = 3

 EXPORTING
   OUT_LINE1                 = 
   OUT_LINE2                 =
   OUT_LINE3                 =
* TABLES
*   OUT_LINES                 =
* EXCEPTIONS
   OUTPUTLEN_TOO_LARGE       = 1
   OTHERS                    = 2

Many thanks

ama

0 Kudos
383

Hi, Muhammed Aftab Alam

My Above code didnu2019t solve your problem? If not please tell me you else requirements.

Thanks and Kind Regards,

Faisal

Former Member
0 Kudos
383

hi use this FM RKD_WORD_WRAP

Former Member
0 Kudos
383

Instead of saving into different variables..try using an internal table

Take one column in this table of type string.

Then use

SPLIT command into internal table.

This will split the string as you want and specifying the internal table would fetch all the split strings into an internal table.

Hope this helps

Regards,

Prashant

Former Member
0 Kudos
383

Use FM TR_SPLIT_TEXT

Former Member
0 Kudos
383

check these fms

STRING_SPLIT

SWA_STRING_SPLIT

TEXT_SPLIT

кu03B1ятu03B9к

Former Member
0 Kudos
383

Hi,

thanks for your suggestions. Since I am a newbie, it'll take some time to implement them.

Regards

ama

faisal_altaf2
Active Contributor
0 Kudos
384

Hi, Muhammed Aftab Alam

Test the following Sample code it working the same way you are requiring, I have Tested,

REPORT zfsl_sub_string.

DATA: main_string TYPE string,
      len_string TYPE i,
      reminder TYPE i,
      string_from TYPE i.
main_string = 'Hallo SAP World'. " You can give any string here.
len_string = STRLEN( main_string ).

DO 3 TIMES.
  reminder = len_string MOD 3.
  IF reminder NE 0.
    CONCATENATE main_string ' ' into main_string SEPARATED BY space.
  len_string = strlen( main_string ).
  ENDIF.
ENDDO.
len_string = len_string div 3.

string_from = 0.
DO len_string TIMES.
  WRITE: / main_string+string_from(3).
  ADD 3 TO : string_from.
ENDDO.

Following too the same as above just add PARAMETER for input String

REPORT zfsl_sub_string.

PARAMETERS: pstring TYPE string.

DATA: "pstring TYPE string,
      len_string TYPE i,
      reminder TYPE i,
      string_from TYPE i.
*pstring = 'Hallo SAP World'. " You can give any string here.
len_string = STRLEN( pstring ).

DO 3 TIMES.
  reminder = len_string MOD 3.
  IF reminder NE 0.
    CONCATENATE pstring ' ' into pstring SEPARATED BY space.
  len_string = strlen( pstring ).
  ENDIF.
ENDDO.
len_string = len_string div 3.

string_from = 0.
DO len_string TIMES.
  WRITE: / pstring+string_from(3).
  ADD 3 TO : string_from.
ENDDO.

Please Reply if any Problem,

Kind Regards,

Faisal

Edited by: Faisal Altaf on Jan 27, 2009 3:06 PM

0 Kudos
383

Hallo Faisal,

it works fine, but the substrings must be saved in different variables, i.e. var1=Hall, var2= lo (first place of var2 is space) and so on

Kind regards,

ama

0 Kudos
383

Hi, Muhammed Aftab Alam

Test the following code here i am appending these sub string to an internal table.

PARAMETERS: pstring TYPE string.

TYPES: BEGIN OF t_it,
  sub_string(3),
  END OF t_it.

DATA: it TYPE STANDARD TABLE OF t_it WITH HEADER LINE,
      len_string TYPE i,
      reminder TYPE i,
      string_from TYPE i.
*pstring = 'Hallo SAP World'. " You can give any string here.
len_string = STRLEN( pstring ).

DO 3 TIMES.
  reminder = len_string MOD 3.
  IF reminder NE 0.
    CONCATENATE pstring ' ' into pstring SEPARATED BY space.
  len_string = strlen( pstring ).
  ENDIF.
ENDDO.
len_string = len_string div 3.

string_from = 0.
DO len_string TIMES.
  it-sub_string = pstring+string_from(3).
  "WRITE: / pstring+string_from(3).
  ADD 3 TO : string_from.
  APPEND it to it.
ENDDO.

LOOP AT it into it.

WRITE: / it.

ENDLOOP.

Please Reply if any Question

Kind Regards,

Faisal

0 Kudos
383

Hallo Faisal,

thanks for the rapid reply. How can i call a particular substring? Since I have to do some functions with the substrings, they must be save in different veriables. Sorry for asking samll things.

Many thanks

ama

0 Kudos
383

Hi, Muhammed Aftab Alam

Suppose you give the string 'Hallo SAP World' it will divide this String to 5 sub Strings and in the other world when you talk about the Internal Table it will Append 5 Lines to table. Now you can read any line from internal table giving the specific INDEX Number. Using following READ TABLE.

READ TABLE it INTO it INDEX 4.
WRITE: / it-sub_string.

The above code will Return you the 4th Sub String

Kind Regards,

Faisal

0 Kudos
383

Hallo,

many thanks. Problem is solved.

Kind regards

ama