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: 

'SWA_STRING_SPLIT' splitting problem.

zhlish
Explorer
0 Kudos
1,512

I'm using the FM 'SWA_STRING_SPLIT' to split a string of 255.
While debugging I noticed that it doesn't split at the maximum of length that I gave, but instead like in the screenshot I attached.
What I'm trying to do is to split the first part when reach length 128 (the limit length of ALV) and insert it into field HEADER1, and the rest of the string into field HEADER1_SPLIT.

I can actually add another field to show the rest of the string, but I prefer to keep just 2.

LOOP AT gt_out.
CALL FUNCTION 'SWA_STRING_SPLIT'
EXPORTING
input_string = lv_testo1
max_component_length = 128
* TERMINATING_SEPARATORS =
* OPENING_SEPARATORS =
TABLES
string_components = it_split1
EXCEPTIONS
max_component_length_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

LOOP AT it_split1.
READ TABLE it_split1 INDEX 1.
IF sy-subrc IS INITIAL.
gt_out-header1 = it_split1-str.
ENDIF.

READ TABLE it_split1 INDEX 2.
IF sy-subrc IS INITIAL.
gt_out-header1_split = it_split1-str.
ENDIF.

ENDLOOP.
*
MODIFY gt_out.
1 ACCEPTED SOLUTION

matt
Active Contributor
1,305
DATA long TYPE c LENGTH 255.
DATA first_128 TYPE c LENGTH 128.<br>DATA rest TYPE c LENGTH 127.
first_128 = long.
rest = long+128.

Why use a FM?

Or if they're all strings.

DATA long TYPE string.
DATA first_128 TYPE string.
DATA rest TYPE string.
first_128 = long(128).
rest = long+128.

Or you can use string functions

DATA long TYPE string.
DATA first_128 TYPE string.
DATA rest TYPE string.
first_128 = substring( val = long len = 128 ).
rest = substring( val = long off = 128 ).

Of course you must do additional work to cater for long being less than or equal 128 characters long. You can use strlen for this.

6 REPLIES 6

Sandra_Rossi
Active Contributor
1,305

I don't know the goal of the function module SWA_STRING_SPLIT so I can't answer for it.

If you want to split at exactly 128 characters without any other rule, you may use SWA_STRING_TO_TABLE (among probably many others), .

data itab2 type table of text128.
data(xml_out) = `jfjk  fkf  ...`.
CALL FUNCTION 'SWA_STRING_TO_TABLE'
     EXPORTING
          CHARACTER_STRING = XML_OUT
     IMPORTING
          CHARACTER_TABLE  = ITAB2.

0 Kudos
1,305

Hi Sandra,
this fm will just cut at exatly 128 characters, but I still need the remaining of the string, so instead of cut I need to split in 2 parts that don't exceed 128 characters each

1,305

sorry, I mean "split", not "cut". It will split into one or many lines.

Answer edited.

matt
Active Contributor
1,306
DATA long TYPE c LENGTH 255.
DATA first_128 TYPE c LENGTH 128.<br>DATA rest TYPE c LENGTH 127.
first_128 = long.
rest = long+128.

Why use a FM?

Or if they're all strings.

DATA long TYPE string.
DATA first_128 TYPE string.
DATA rest TYPE string.
first_128 = long(128).
rest = long+128.

Or you can use string functions

DATA long TYPE string.
DATA first_128 TYPE string.
DATA rest TYPE string.
first_128 = substring( val = long len = 128 ).
rest = substring( val = long off = 128 ).

Of course you must do additional work to cater for long being less than or equal 128 characters long. You can use strlen for this.

0 Kudos
1,305

What a simple solution, I had focused so much on this FM and the reason why it doesn't work that I completely forgot about the offset..

Now the problem is resolved, but I still don't understand why this FM doesn't fill the maximum component I gave and then split, but instead cut off before it reach that length.

Sandra_Rossi
Active Contributor
1,305

zhlish That's the problem to use unreleased and undocumented function modules, you don't know what they do, and there is the risk that they are changed or removed in any release.