How To Split A String Into 4 Strings of Equal Leng... - SAP Community
How To Split A String Into 4 Strings of Equal Leng... - SAP Community
- SAP Community
Andre_Fischer
Product and Topic Expert
09-05-2018 11:09 AM
22 Kudos
I ran into this when developing the new tasklist for the mass maintenance of OData services (see my
blog Custom tasklist for OData service mass maintenance).
Since I am calling API's that do not return the root cause of an error I wrote some code that retrieved the long
text of the original error.
The problem was then that I had to call a method to add a T100 message to a log file that required 4
parameters of type CHAR50.
So I had to cut the string into 4 pieces each having the length of 50 characters.
Let's have a look what would happen if the incoming string has a length of 102 characters.
First I tried the function module 'SWA_STRING_SPLIT' that takes a string and the desired length of the
substring as an Input parameter. The split string is returned via the table lt_string_comp.
You then still have the problem to find out the number of table entries and you cannot access the fourth table
entry lt_string_comp[ 4 ] if the table has only three entries if the string has a length of 102 characters.
The built in function substring has the disadvantage that it throws an error of
type cx_sy_range_out_of_bounds if it would reach the statement
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 1/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
Though it is not recommended the following code works like a charm. If the string has a length of 102
characters lv7 would contain 2 characters and lv8 would simply be intial.
DATA(lv5) = lv_longtext(50).
DATA(lv6) = lv_longtext+50(50).
DATA(lv7) = lv_longtext+100(50).
DATA(lv8) = lv_longtext+150(50).
Another option was mentioned by matthew.billingham as a comment to this blog. By calling the
method set_msg_vars_for_clike of class cl_message_helper you get four strings of length 50 characters as
well.
This method under the hood uses the old fashioned ABAP syntax
as well.
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 2/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
In the following I move the long text to a structure with 4 fields of length 50 characters each.
As a result error messages longer than 200 characters are truncated but no exception is thrown opposed to
using substring.
And it does not make use of out of date ABAP coding style.
Moreover you would be able to define a structure with fields that have different lengths, say 10, 24, 40 and 5
characters. A string moved to this structure would be split approriately without having to look for exceptions
whose handling is time consuming and not needed in this case.
ls_longtext = lx_cof->get_longtext( ).
If you want to play around I have created the following test report that shows four of the five approaches
described above.
*&---------------------------------------------------------------------*
*& Report z_split_string
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_split_string.
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 3/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
lv_longtext = lv_teststring.
ls_longtext = lv_teststring.
TRY.
DATA(lv1) = substring( val = lv_longtext off = 0 len = 50 ).
DATA(lv2) = substring( val = lv_longtext off = 50 len = 50 ).
DATA(lv3) = substring( val = lv_longtext off = 100 len = 50 ).
DATA(lv4) = substring( val = lv_longtext off = 150 len = 50 ).
CATCH cx_sy_range_out_of_bounds INTO DATA(lx_too_long).
data(lv_error_lx_too_long) = lx_too_long->get_text( ).
ENDTRY.
DATA(lv5) = lv_longtext(50).
DATA(lv6) = lv_longtext+50(50).
DATA(lv7) = lv_longtext+100(50).
DATA(lv8) = lv_longtext+150(50).
cl_message_helper=>set_msg_vars_for_clike( lv_longtext ).
data(out) = cl_demo_output=>new( ).
data = ls_longtext-msgv3 ),
data = ls_longtext-msgv4 ),
data = 'use cl_message_helper' ),
data = sy-msgv1 ),
data = sy-msgv2 ),
data = sy-msgv3 ),
data = sy-msgv4
)->display( ).
Substring access (offset = 100, length = 50) to a data object of the size 102 exceeds valid bo
********************************
use of substring
01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789
Field
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 5/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
Field
01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789
01
Field
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 6/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789
01
Field
use cl_message_helper
01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789
01
Field
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 7/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
matt
Active Contributor
09-05-2018 11:33 AM
6 Kudos
cl_message_helper=>set_msg_vars_for_clike( my_text ).
WRITE / : sy-msgv1, sy-msgv2, sy-msgv3, sy-msgv4.
Andre_Fischer
Product and Topic Expert
09-05-2018 12:30 PM
0 Kudos
Hi Matthew,
thanks for pointing me to this option. I therefore changed the description for the approach using a structure
from ""the best..." to "the most flexible approach". ;-).
Best Regards,
Andre
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 8/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
matt
Active Contributor
09-05-2018 1:40 PM
1 Kudo
It’s more flexible if you’re wishing to do a quick concatenation, and the structure won’t change. This approach
was used quite often pre-unicode. The problem that can arise is if a structure were to change so that it
incorporated a non-clike field. It just doesn’t work then.
Looking at your problem as stated (and without knowing the full details obviously), if I’d encountered it, I’d
have looked here:
The problem was then that I had to call a method to add a T100 message to a log file that required 4
parameters of type CHAR50.
The solution (if possible) would be to add a method so a string can be added to the log file. By doing this you
have simpler calling code, and you’ve made your logging class more powerful.
Another hint -> as horst.keller explains the ABAP documentation uses the class CL_DEMO_OUTPUT for
sample code. You might like to look at that.
btw, SAP’s own ABAP Guidelines and DSAG’s latest best practice document, speak against using prefixes as
type indicators such as l, s etc. Especially as not one of the variables in your example program is, in fact,
local…
Andre_Fischer
Product and Topic Expert
09-05-2018 1:59 PM
0 Kudos
Hi Matthew,
the class CL_DEMO_OUTPUT is nice but I am not allowed to use it in SAP production code since it has been
released for demo purposes only.
Unfortunately I am not the owner of the logging class, but I will ask the colleagues what they can do.
Regards,
Andre
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 9/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
matt
Active Contributor
09-05-2018 2:08 PM
2 Kudos
I meant instead of using the WRITE statements in your simple test report. I kind of assumed that wasn't
intended for production!
Jelena
Active Contributor
09-05-2018 4:14 PM
0 Kudos
Thanks for sharing! I'm wondering if you also thought about a more generic option. E.g. in this case we're
splitting into 50 characters and pre-define 4 variables. But what if it's not 50 but 10 or 20 characters? How
would you approach that?
Andre_Fischer
Product and Topic Expert
09-05-2018 4:18 PM
0 Kudos
done
Jelena
Active Contributor
09-05-2018 4:18 PM
0 Kudos
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 10/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
TBH personally I find the use of CL_DEMO_OUTPUT in the examples annoying. If neither is suitable to be
copy-pasted for production use then might as well just use WRITE. Simpler and shorter.
Even if I can understand rationale behind this it's still irritating and for some reason looks rather pompous.
Can't help it.
Andre_Fischer
Product and Topic Expert
09-05-2018 4:22 PM
0 Kudos
Hi Jelena,
I would just create an appropriate structure (here with 10, 20, 30 and 40 characters).
0123456789
01234567890123456789
012345678901234567890123456789
0123456789012345678901234567890123456789
Jelena
Active Contributor
09-05-2018 5:18 PM
0 Kudos
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 11/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
Right but this is just another sort of hard-coded variation. What about, say, using a number N as a parameter
and a more generic code to split into however many pieces needed (each of N length) within some reasonable
limit (to make it easier)? Don't have a specific use case in mind, just idle curiosity.
matt
Active Contributor
09-06-2018 5:59 AM
2 Kudos
horst_keller
Product and Topic Expert
09-06-2018 11:11 PM
3 Kudos
Well, then show me how you translate CL_DEMO_OUTPUT=>DISPLAY( itab ) to a WRITE statement ...
former_member428487
Explorer
09-07-2018 5:58 PM
0 Kudos
Jelena
Active Contributor
09-07-2018 8:12 PM
1 Kudo
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 12/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
Touche. But in the simplest cases it's still feels like "ugh".
Jelena
Active Contributor
09-07-2018 8:14 PM
3 Kudos
Not that it ever stopped anyone but FYI this function is not released.
horst_keller
Product and Topic Expert
09-08-2018 6:16 AM
1 Kudo
But only this "pompous ugh" makes the examples directly executable in the web version of the
documentation, notably in ADT!
peisenhauer
Explorer
10-25-2018 2:51 PM
0 Kudos
In my opinion a quick "while construct" cutting the pieces out of the string would do the trick. Reusing some
code I already wrote for a BAPI_MATERIAL_SAVEDATA call (extensionin..) I think of the following:
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 13/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
* the length to be processed at once can have 2 different values. The full single_unit_len len
DATA(unit_len) = COND #(
WHEN remaining_strlen > single_unit_len THEN single_unit_len
ELSE remaining_strlen
).
* copy a single unit out of the string.
DATA(single_unit) = substring(
val = string
len = unit_len
).
* "cut" the copied out unit out of the string
string = substring( val = string off = unit_len len = remaining_strlen - unit_len ).
* get the new remaining length.
remaining_strlen = strlen( string ).
* append the single unit to a string table.
collected_strings = VALUE #( BASE collected_strings ( single_unit ) ).
ENDWHILE.
cl_demo_output=>display( collected_strings ).
shais
Participant
10-31-2018 9:38 AM
1 Kudo
For Unicode support (with non C-Like types) you may use CL_ABAP_CONTAINER_UTILITIES-
>READ_CONTAINER_C.
former_member329502
Explorer
09-17-2020 10:25 AM
1 Kudo
nice blog Sir, can I translate and post a new blog in China CSDN site with a reference to yours?
Andre_Fischer
Product and Topic Expert
09-17-2020 10:30 AM
2 Kudos
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 14/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
Sure.
Best regards,
Andre
GManousaridis
Participant
10-07-2021 8:51 PM
0 Kudos
Hello Andre,
DATA(lv_counter) = 2.
ASSIGN lv_msgv1 TO <lv_msgv>.
LOOP AT lt_msg_words ASSIGNING FIELD-SYMBOL(<lv_word>).
IF strlen( <lv_msgv> && <lv_word> ) > 50.
DATA(lv_msg_var) = |lv_msgv{ lv_counter }|.
ASSIGN (lv_msg_var) TO <lv_msgv>.
ADD 1 TO lv_counter.
ENDIF.
<lv_msgv> = COND #( WHEN <lv_msgv> IS INITIAL THEN <lv_word> ELSE |{ <lv_msgv> } { <lv_w
IF lv_counter > 4.
EXIT.
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 15/16
2/5/24, 12:51 How to split a string into 4 strings of equal leng... - SAP Community
ENDIF.
ENDLOOP.
Cheers,
George
GManousaridis
Participant
10-07-2021 9:04 PM
0 Kudos
Hi Matthew,
Great approach! I tried it and a problem I saw, was that the method splits the text in exactly 50 chars, so it
breaks the words in that position.
Other than that, which prevented me to use it in my case, it works like a charm!
Cheers,
George
https://community.sap.com/t5/technology-blogs-by-sap/how-to-split-a-string-into-4-strings-of-equal-length-in-abap/ba-p/13368900 16/16