-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathzcl_demo_abap_string_proc.clas.abap
More file actions
2751 lines (2198 loc) · 101 KB
/
zcl_demo_abap_string_proc.clas.abap
File metadata and controls
2751 lines (2198 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"! <p class="shorttext"><strong>String processing</strong><br/>ABAP cheat sheet example class</p>
"!
"! <p>The example class demonstrates various syntax options for processing character strings.<br/>
"! Choose F9 in ADT to run the class.</p>
"!
"! <h2>Note</h2>
"! <p>Find the following information in the ABAP Doc comment of class {@link zcl_demo_abap_aux}:</p>
"! <ul><li>How to get started with the example class</li>
"! <li>Structuring of (most of) the example classes</li>
"! <li>Disclaimer</li></ul>
CLASS zcl_demo_abap_string_proc DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES: if_oo_adt_classrun.
METHODS:
m01_create_strings_assign_val IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m02_chain_strings IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m03_string_templates IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m04_string_templates_format IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m05_string_length IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m06_concatenate IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m07_literal_operator IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m08_split IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m09_lowercase_uppercase IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m10_shift IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m11_condense IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m12_reverse IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m13_insert_substrings IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m14_overlay IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m15_process_substrings IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m16_search_characters IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m17_replace_characters IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m18_search_substrings_comp_op IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m19_find_statement IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m20_find_in_table IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m21_find_function IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m22_replace_statement IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m23_replace_section IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m24_replace_in_table IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m25_replace_function IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m26_search_pattern_logical_op IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m27_search_regex IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m28_replacements_using_regex IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m29_regex_system_classes IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m30_distance_function IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m31_repeat_function IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m32_cmin_cmax_functions IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m33_escape_function IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m34_string_processing_xco IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string,
m35_byte_string_processing IMPORTING out TYPE REF TO if_oo_adt_classrun_out text TYPE string.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_demo_abap_string_proc IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
zcl_demo_abap_aux=>set_example_divider(
out = out
text = `ABAP Cheat Sheet Example: String Processing`
).
"Dynamically calling methods of the class
"The method names are retrieved using RTTI. For more information, refer to the
"Dynamic Programming ABAP cheat sheet.
"Only those methods should be called that follow the naming convention M + digit.
DATA(methods) = CAST cl_abap_classdescr( cl_abap_typedescr=>describe_by_object_ref( me ) )->methods.
SORT methods BY name ASCENDING.
"To call a particular method only, you can comment in the WHERE clause and
"adapt the literal appropriately.
LOOP AT methods INTO DATA(meth_wa)
"WHERE name CS 'M01'
.
TRY.
"The find function result indicates that the method name begins (offset = 0) with M and a digit.
IF find( val = meth_wa-name pcre = `^M\d` case = abap_false ) = 0.
CALL METHOD (meth_wa-name) EXPORTING out = out text = CONV string( meth_wa-name ).
ENDIF.
CATCH cx_root INTO DATA(error).
out->write( error->get_text( ) ).
ENDTRY.
ENDLOOP.
ENDMETHOD.
METHOD m01_create_strings_assign_val.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Creating Strings and Assigning Values| ).
"Data object declarations providing default values
DATA: flag TYPE c LENGTH 1 VALUE 'X', "Single quotes
str_a1 TYPE string VALUE `Hallo, how are you?`. "Backquotes
DATA: char_a1 TYPE c LENGTH 5,
str_a2 TYPE string,
str_a3 LIKE str_a2.
"Examples for type n
DATA zip_code TYPE n LENGTH 5 VALUE '12345'.
DATA isbn_number TYPE n LENGTH 13 VALUE '1234567890123'.
"Value assignments to existing data objects
char_a1 = 'ab123'.
str_a2 = `<p>Hallo!</p>`.
"Escaping a backquote using another backquote
str_a3 = `This is a backquote: ``.`.
"If possible, avoid unnecessary type conversion; in principle,
"every convertible type can be specified
"Assigning a fixed-length string to a variable-length string.
str_a2 = 'abc'.
DATA str_a4 TYPE string VALUE 'X'. "Type c length 1
DATA str_a5 TYPE string VALUE -1. "Type i
"Inline declaration: data object declaration and
"value assignment
"Data type is automatically derived
DATA(char_a2) = 'abcd'. "Type c length 4
DATA(str_a6) = `efgh`. "Type string
"Note: Variable is of type c length 4. Characters are truncated.
"In newer ABAP releases, the following statement shows a syntax
"warning that the value of the literal (intentionally specified
"here like this) is not an admissable value for the target type.
"Therefore, the example is provided differently to circumvent the
"syntax warning.
"char_a2 = 'ijklmnopq'.
TYPES c_l9 TYPE c LENGTH 9.
DATA some_char TYPE c_l9 VALUE 'ijklmnopq'.
char_a2 = some_char.
"Treating trailing blanks
DATA(char_a3) = 'ab '.
DATA(str_a7) = `cdefgh`.
str_a7 = char_a3. "Trailing blanks are not respected.
"Excursion: Chaining strings
"Note the conversion result of str_a5 above (i to string)
DATA(str_a8) = str_a4 && ` ` && str_a5 && `!`.
out->write( data = str_a3 name = `str_a3` ).
out->write( |\n| ).
out->write( data = char_a2 name = `char_a2` ).
out->write( |\n| ).
out->write( data = str_a7 name = `str_a7` ).
out->write( |\n| ).
out->write( data = str_a8 name = `str_a8` ).
ENDMETHOD.
METHOD m02_chain_strings.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Chaining Strings| ).
DATA(str_b1) = `Hallo`.
DATA(str_b2) = `how`.
DATA(str_b3) = `are`.
"Chaining using && operator
DATA(str_b4) = str_b1 && ` ` && sy-uname && `, ` && str_b2 && ` ` && str_b3 && ` you?`.
"Chaining only character literals of the same type using & operator
"Note: Such a combination of literals is possible up to 255 chars.
DATA(char_b1) = 'AB' & 'AP '. "Trailing blanks are ignored
DATA(str_b5) = `AB` & `AP `.
out->write( data = str_b4 name = `str_b4` ).
out->write( |\n| ).
out->write( data = char_b1 name = `char_b1` ).
ENDMETHOD.
METHOD m03_string_templates.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: String Templates| ).
"--- Constructing Strings ---
"The expression must be convertible to a string. A blank (not
"within the curly brackets) means a blank in the resulting string.
DATA(str_c1) = `Hallo`.
DATA(str_c2) = `how`.
DATA(str_c3) = `are`.
DATA(str_c4) = |{ str_c1 } { sy-uname }, | &&
|{ str_c2 } { str_c3 } you?|.
out->write( data = str_c4 name = `str_c4` ).
"--- String Templates (2): Control Characters ---
"Interpretation of character combinations as control characters
"\n interpreted as a line break
DATA(str_c5) = |{ str_c1 }\n{ sy-uname },| &&
|\n{ str_c2 }\n{ str_c3 }\nyou?|.
out->write( data = str_c5 name = `str_c5` ).
out->write( |\n| ).
"Excursion: Class CL_ABAP_CHAR_UTILITIES provides attributes and methods as utilities for string processing.
"See the class documentation.
"The following examples demonstrate that attributes that contain control characters can be replaced by
"a representation of control characters in a string template.
DATA(str_c6) = |{ str_c1 }{ cl_abap_char_utilities=>newline }{ sy-uname }|.
DATA(str_c7) = |{ str_c1 }\n{ sy-uname }|.
DATA(str_c8) = |{ str_c1 }{ cl_abap_char_utilities=>horizontal_tab }{ sy-uname }|.
DATA(str_c9) = |{ str_c1 }\t{ sy-uname }|.
DATA(str_c10) = |{ str_c1 }{ cl_abap_char_utilities=>cr_lf }{ sy-uname }|.
DATA(str_c11) = |{ str_c1 }\r\n{ sy-uname }|.
ASSERT str_c10 = str_c11.
out->write( data = str_c6 name = `str_c6` ).
out->write( |\n| ).
out->write( data = str_c7 name = `str_c7` ).
out->write( |\n| ).
out->write( data = str_c8 name = `str_c8` ).
out->write( |\n| ).
out->write( data = str_c9 name = `str_c9` ).
ENDMETHOD.
METHOD m04_string_templates_format.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: String Templates: Formatting Options| ).
*&---------------------------------------------------------------------*
*& DATE
*&---------------------------------------------------------------------*
"Defining the format of a date
"The output is just an example and depends on your settings.
DATA(d) = |The date is { cl_abap_context_info=>get_system_date( ) DATE = USER }.|. "The date is 01/01/2024.
d = |{ cl_abap_context_info=>get_system_date( ) DATE = RAW }|. "20240101
d = |{ cl_abap_context_info=>get_system_date( ) DATE = ISO }|. "2024-01-01
d = |{ cl_abap_context_info=>get_system_date( ) DATE = ENVIRONMENT }|. "01/01/2024
*&---------------------------------------------------------------------*
*& TIME
*&---------------------------------------------------------------------*
"Defining the format of a time
"The output is just an example and depends on your settings.
DATA(tm) = |The time is { cl_abap_context_info=>get_system_time( ) TIME = ISO }.|. "The time is 14:37:24.
tm = |{ cl_abap_context_info=>get_system_time( ) TIME = RAW }|. "143724
tm = |{ cl_abap_context_info=>get_system_time( ) TIME = USER }|. "14:37:24
tm = |{ cl_abap_context_info=>get_system_time( ) TIME = ENVIRONMENT }|. "14:37:24
*&---------------------------------------------------------------------*
*& TIMESTAMP
*&---------------------------------------------------------------------*
"Defining the format of a time stamp
"The output is just an example and depends on your settings.
DATA(ts) = |{ utclong_current( ) TIMESTAMP = SPACE }|. "2024-01-01 14:39:50.4069170
ts = |{ utclong_current( ) TIMESTAMP = ISO }|. "2024-01-01T14:39:50,4071110
ts = |{ utclong_current( ) TIMESTAMP = USER }|. "01/01/2024 14:39:50.4072010
ts = |{ utclong_current( ) TIMESTAMP = ENVIRONMENT }|. "01/01/2024 14:39:50.4073230
ts = |{ utclong_current( ) }|. "2024-01-01 14:39:50.4074060
*&---------------------------------------------------------------------*
*& TIMEZONE
*&---------------------------------------------------------------------*
"Defining the format of a time stamp using the rules for time zones
DATA(tz) = |{ utclong_current( ) TIMEZONE = 'UTC' }|. "2024-12-30 14:43:20.6534640
tz = |{ utclong_current( ) TIMEZONE = 'CET' COUNTRY = 'DE ' }|. "30.12.2024 15:43:20,6536320
tz = |{ utclong_current( ) TIMEZONE = 'EST' COUNTRY = 'US ' }|. "12/30/2024 09:43:20.6889180 AM
*&---------------------------------------------------------------------*
*& CASE
*&---------------------------------------------------------------------*
"Lowercase and uppercase
DATA s1 TYPE string.
DATA s2 TYPE string.
s1 = `AbCdEfG`.
s2 = |{ s1 CASE = LOWER }|. "abcdefg
s2 = |{ s1 CASE = UPPER }|. "ABCDEFG
*&---------------------------------------------------------------------*
*& WIDTH/ALIGN
*&---------------------------------------------------------------------*
s1 = `##`.
s2 = |{ s1 WIDTH = 10 ALIGN = LEFT }<---|. "'## <---'
s2 = |{ s1 WIDTH = 10 ALIGN = CENTER }<---|. "' ## <---'
*&---------------------------------------------------------------------*
*& PAD
*&---------------------------------------------------------------------*
"Used to pad any surplus places in the result with the specified character.
s2 = |{ s1 WIDTH = 10 ALIGN = RIGHT PAD = `.` }<---|. "'........##<---'
*&---------------------------------------------------------------------*
*& DECIMALS
*&---------------------------------------------------------------------*
s1 = |{ CONV decfloat34( - 1 / 3 ) DECIMALS = 3 }|. "'-0.333'
*&---------------------------------------------------------------------*
*& SIGN
*&---------------------------------------------------------------------*
"Defining the format of the +/- sign when the string represented
"by the embedded expression represents a numeric value
"- left without space, no +
s1 = |{ +1 SIGN = LEFT }|. "1
"- and + left without space
s1 = |{ 1 SIGN = LEFTPLUS }|. "+1
"- left without space, blank left for +
s1 = |{ 1 SIGN = LEFTSPACE }|. " 1
"- right without space, no +
s1 = |{ -1 SIGN = RIGHT }|. "1-
"- and + right without space
s1 = |{ 1 SIGN = RIGHTPLUS }|. "1+
"- left without space, blank right for +
s1 = |{ +1 SIGN = RIGHTSPACE }|. "1
*&---------------------------------------------------------------------*
*& ZERO
*&---------------------------------------------------------------------*
"Defining the format of the numeric value zero.
"Only to be specified if the embedded expression has a numeric data type.
s1 = |'{ 0 ZERO = NO }' and '{ 0 ZERO = YES }'|. "'' and '0'
*&---------------------------------------------------------------------*
*& XSD
*&---------------------------------------------------------------------*
"Formatting is applied to an embedded expression (elementary data types) in asXML format that is
"assigned to its data type. Check the information in the ABAP Keyword Documentation about the asXML
"mapping of elementary ABAP types.
DATA xstr TYPE xstring VALUE `41424150`.
DATA dat TYPE d VALUE '20240101'.
DATA tim TYPE t VALUE '123456'.
DATA(utc) = utclong_current( ). "e.g. 2024-01-01 13:51:38.5708800
s1 = |{ xstr XSD = YES }|. "QUJBUA==
s1 = |{ dat XSD = YES }|. "2024-01-01
s1 = |{ tim XSD = YES }|. "12:34:56
s1 = |{ utc XSD = YES }|. "2024-01-01T13:51:38.57088Z
*&---------------------------------------------------------------------*
*& STYLE
*&---------------------------------------------------------------------*
"Defining the style of decimal floating point numbers;
"see the details in the ABAP Keyword Documentation.
DATA(dcfl34) = CONV decfloat34( '-123.45600' ).
s1 = |{ dcfl34 }|. "-123.456
"Creates the predefined format
s1 = |{ dcfl34 STYLE = SIMPLE }|. "-123.456
"+/- added to the right, removes trailing zeros
s1 = |{ dcfl34 STYLE = SIGN_AS_POSTFIX }|. "123.456-
"Retains trailing zeros
s1 = |{ dcfl34 STYLE = SCALE_PRESERVING }|. "-123.45600
"Scientific notation; at least a two digit exponent with a plus/minus sign
s1 = |{ dcfl34 STYLE = SCIENTIFIC }|. "-1.23456E+02
"Scientific notation; only one integer digit with the value 0
s1 = |{ dcfl34 STYLE = SCIENTIFIC_WITH_LEADING_ZERO }|. "-0.123456E+03
"Scientific notation; exponent has 3 digits for decfloat16 and 4 digits for decfloat34
s1 = |{ dcfl34 STYLE = SCALE_PRESERVING_SCIENTIFIC }|. "-1.2345600E+0002
"Technical format
s1 = |{ dcfl34 STYLE = ENGINEERING }|. "-123.456E+00
*&---------------------------------------------------------------------*
*& ALPHA
*&---------------------------------------------------------------------*
"Adds or removes leading zeros from strings of digits; the data type
"must be string, c, or n
"Adding leading zeros
"Additionally specifying WIDTH
"Note: The specified length is only used if it is greater than
"the length of provided string (without leading zeros)
s1 = |{ '1234' ALPHA = IN WIDTH = 10 }|. "0000001234
s1 = |{ '00000000000000000000000012' ALPHA = IN WIDTH = 10 }|. "0000000012
"Fixed-length string provided, WIDTH not specified
s1 = |{ ' 12' ALPHA = IN }|. "00012
"Removing leading zeros
s1 = |{ '00001234' ALPHA = OUT }|. "1234
"Do not apply formatting
s1 = |{ '00001234' ALPHA = RAW }|. "00001234
*&---------------------------------------------------------------------*
*& EXPONENT
*&---------------------------------------------------------------------*
"Defining the exponent when formatting floating point numbers;
"only usable with numeric data types; only affects data type f
"or in combination with STYLE = scientific
DATA(flp) = CONV f( 1 / 3 ).
"0.33333333333333331
s1 = |{ flp }|.
"3.3333333333333331E-01
s1 = |{ flp EXPONENT = -1 }|.
"0.000000000033333333333333331E+10
s1 = |{ flp EXPONENT = 10 }|.
DATA(dec_num) = CONV decfloat34( '-123.45600' ).
"-1.23456E+02
s1 = |{ dec_num STYLE = SCIENTIFIC }|.
"-1234.56E-01
s1 = |{ dec_num STYLE = SCIENTIFIC EXPONENT = -1 }|.
*&---------------------------------------------------------------------*
*& CURRENCY
*&---------------------------------------------------------------------*
"Defining the number of decimal places in the context of currencies;
"only usable with numeric data types; some options such as DECIMALS
"cannot be specified together with CURRENCY, SIGN can be specified
"for +/-; check descriptions of the behavior of the various numeric
"types in the ABAP Keyword Documentation
"Type i: A decimal separator is inserted at the place determined by
"the currency
"123456.78
s1 = |{ 12345678 CURRENCY = 'EUR' }|.
"Type f: Same effect as DECIMALS, number of decimal places is
"determined by the currency
"1.23
s1 = |{ CONV f( '1.234' ) CURRENCY = 'EUR' }|.
"Using CURRENCY and SIGN
"+123.45
s1 = |{ 12345 CURRENCY = 'EUR' SIGN = LEFTPLUS }|.
*&---------------------------------------------------------------------*
*& COUNTRY
*&---------------------------------------------------------------------*
"Temporarily setting country-specific formatting (alternative to using
"the ENVIRONMENT option)
DATA land TYPE land1 VALUE 'DE '.
"987.654.321 (for example)
s1 = |{ 987654321 COUNTRY = land }|.
"987,654,321 (for example)
s1 = |{ 987654321 COUNTRY = 'US ' }|.
"10/18/2025 (for example)
s1 = |{ cl_abap_context_info=>get_system_date( ) COUNTRY = 'US ' }|.
"14:21:12 (for example)
s1 = |{ cl_abap_context_info=>get_system_time( ) COUNTRY = land }|.
"Using non-existing country
land = '&/§'.
TRY.
s1 = |{ 987654321 COUNTRY = land }|.
CATCH cx_sy_strg_format INTO DATA(err).
s1 = err->get_text( ).
ENDTRY.
*&---------------------------------------------------------------------*
*& NUMBER
*&---------------------------------------------------------------------*
"Defining the format of decimal representation (decimal and thousands
"separators); only usable with numeric data types; only specific formats
"can be specified (RAW being the default)
DATA decimal_number TYPE decfloat34 VALUE '-123456.7890'.
"Period as decimal separator, no thousands separators
"-123456.789
s1 = |{ decimal_number NUMBER = RAW }|.
"Decimal/thousands separators based on user master record
"-123.456,789 (for example)
s1 = |{ decimal_number NUMBER = USER }|.
"Decimal/thousands separators based on current language
"environment
"-123.456,789 (for example)
s1 = |{ decimal_number NUMBER = ENVIRONMENT }|.
out->write( zcl_demo_abap_aux=>no_output ).
ENDMETHOD.
METHOD m05_string_length.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Determining the Length of Strings| ).
DATA(str_e1) = `abc def ghi `.
DATA(char_e1) = 'abc def ghi '.
"strlen
"Result depends on the type of the data object
"Fixed-length string ignores trailing blanks
DATA(length_e1) = strlen( str_e1 ).
DATA(length_e2) = strlen( char_e1 ).
"numofchar
"To exclude trailing blanks in any case.
DATA(length_e3) = numofchar( str_e1 ).
DATA(length_e4) = numofchar( char_e1 ).
"Excursion:
"To emphasizes modern, expression-enabled ABAP, the expression
"with the string function can be placed directly in the DO
"statement instead of having an extra variable.
DATA(str_e3) = `abcde`.
DATA(length_e5) = strlen( str_e3 ).
DATA(int_e1) = 0.
DO length_e5 TIMES.
int_e1 += 1.
ENDDO.
DATA(int_e2) = 0.
DO strlen( str_e3 ) TIMES.
int_e2 += 1.
ENDDO.
out->write( data = length_e1 name = `length_e1` ).
out->write( |\n| ).
out->write( data = length_e2 name = `length_e2` ).
out->write( |\n| ).
out->write( data = length_e3 name = `length_e3` ).
out->write( |\n| ).
out->write( data = length_e4 name = `length_e4` ).
out->write( |\n| ).
out->write( data = int_e1 name = `int_e1` ).
out->write( |\n| ).
out->write( data = int_e2 name = `int_e2` ).
ENDMETHOD.
METHOD m06_concatenate.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Concatenating Strings| ).
DATA(str_f1) = `Hallo`.
DATA(str_f2) = `world`.
"Concatenation using && operator and string templates
DATA(str_f3) = str_f1 && str_f2.
DATA(str_f4) = str_f1 && ` ` && str_f2.
DATA(str_f5) = |{ str_f1 } { str_f2 }|.
"CONCATENATE statements
CONCATENATE str_f1 str_f2 INTO DATA(str_f6).
"Adding a separation sign using the addition SEPARATED BY
CONCATENATE str_f1 str_f2 INTO DATA(str_f7) SEPARATED BY ` `.
CONCATENATE str_f1 str_f2 INTO DATA(str_f8) SEPARATED BY `#`.
DATA(char_f1) = '2 trailing blanks: '.
DATA(char_f2) = '3 trailing blanks: '.
DATA(char_f3) = '<-'.
"Keeping trailing blanks in the result when concatenating
"fixed-length strings. The ones of variable-length strings are
"respected by default
CONCATENATE char_f1 char_f2 char_f3
INTO DATA(char_f4) RESPECTING BLANKS.
"Trailing blanks are ignored
CONCATENATE char_f1 char_f2 char_f3 INTO DATA(char_f5).
"Example use case: Concatenating smaller text fragments
"sequentially into a longer character sequence.
DATA: itab_g TYPE TABLE OF string,
alphabet1 TYPE string.
itab_g = VALUE #( ( `abc` ) ( `def` ) ( `ghi` ) ).
LOOP AT itab_g ASSIGNING FIELD-SYMBOL(<abc>).
alphabet1 = alphabet1 && <abc>.
"Alternative:
"CONCATENATE alphabet <abc> INTO alphabet.
ENDLOOP.
"Avoiding loops if your use case is to concatenate lines of an
"internal table into a string in one go
CONCATENATE LINES OF itab_g INTO DATA(alphabet2).
""Adding a separation sign using the addition SEPARATED BY
CONCATENATE LINES OF itab_g INTO DATA(alphabet3)
SEPARATED BY ` `.
"String function concat_lines_of
DATA(alphabet4) = concat_lines_of( table = itab_g ).
"sep parameter specifying the separation sign
DATA(alphabet5) = concat_lines_of( table = itab_g sep = `,` ).
out->write( data = str_f3 name = `str_f3` ).
out->write( |\n| ).
out->write( data = str_f4 name = `str_f4` ).
out->write( |\n| ).
out->write( data = str_f5 name = `str_f5` ).
out->write( |\n| ).
out->write( data = str_f6 name = `str_f6` ).
out->write( |\n| ).
out->write( data = str_f7 name = `str_f7` ).
out->write( |\n| ).
out->write( data = str_f8 name = `str_f8` ).
out->write( |\n| ).
out->write( data = char_f4 name = `char_f4` ).
out->write( |\n| ).
out->write( data = char_f5 name = `char_f5` ).
out->write( |\n| ).
out->write( data = alphabet1 name = `alphabet1` ).
out->write( |\n| ).
out->write( data = alphabet2 name = `alphabet2` ).
out->write( |\n| ).
out->write( data = alphabet3 name = `alphabet3` ).
out->write( |\n| ).
out->write( data = alphabet4 name = `alphabet4` ).
out->write( |\n| ).
out->write( data = alphabet5 name = `alphabet5` ).
ENDMETHOD.
METHOD m07_literal_operator.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Literal Operator| ).
"Literal operator
"Used to combine character literals of the same type into a single character literal
DATA(abap) = 'AB' & 'AP'.
"Note an upper limit of 255 characters
"If you remove the comment, which results in a combined character literal
"of 256 characters, a syntax error is displayed.
DATA(c_limit_255) =
'##################################################' & "50 x #
'##################################################' &
'##################################################' &
'##################################################' &
'##################################################' &
'#####'
"& '#'
.
"Trailing blanks are respected
DATA(char_with_blanks) = 'AB' & 'AP' & ' '.
"Using RTTI to get type information, retrieving the output length of the combined literal
DATA(output_length) = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_data( char_with_blanks ) )->output_length.
out->write( data = output_length name = `output_length` ).
out->write( |\n| ).
DATA(ch1) = 'AB'.
DATA(ch2) = 'AP'.
"Not possible as the operands are not character literals but data objects
"DATA(combined_ch) = ch1 & ch2.
"Not to be confused with the concatenation operator && to concatenate
"character-like operands; at runtime, any number of character-like operands
"are possible
"The result in the example is of type string.
DATA(combined_ch_with_conc_op) = ch1 && ch2.
"Concatenation similar to the example above
"As the result is of type string using the concatenation operator, the
"trailing blanks are not respected.
DATA(char_with_blanks_conc_op) = 'AB' && 'AP' && ' '.
DATA(len1) = strlen( char_with_blanks_conc_op ).
DATA(len2) = numofchar( char_with_blanks_conc_op ).
out->write( data = len1 name = `len1` ).
out->write( |\n| ).
out->write( data = len2 name = `len2` ).
ENDMETHOD.
METHOD m08_split.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Splitting Strings| ).
DATA(str_g1) = `Hallo,world,12345`.
SPLIT str_g1 AT `,` INTO DATA(str_g2) DATA(str_g3) DATA(str_g4).
"Less data objects than possible splittings
SPLIT str_g1 AT `,` INTO DATA(str_g5) DATA(str_g6).
"Splitting string into an internal table
DATA itab_g1 TYPE TABLE OF string.
SPLIT str_g1 AT ',' INTO TABLE itab_g1.
"Getting the value of a specific segment
DATA(str_g7) = segment( val = str_g1 index = 2 sep = `,` ).
"Example with segment
"A string is split and the values of segments are retrieved. Here,
"all segments are retrieved and inserted into an internal table
"using a DO loop. If you specify an empty string, an exception of
"the class CX_SY_STRG_PAR_VAL is raised. This is true for this
"example since the DO loop inevitably runs into the error because
"of not specifying an appropriate number of loops. Note that
"if the index parameter of the segment function is positive, the
"occurrences are counted from the left. If index is negative, the
"occurrences are counted from the right.
DATA itab_g2 TYPE TABLE OF string.
DO.
TRY.
DATA(str_g8) = segment( val = str_g1
index = sy-index
sep = `,` ).
APPEND |Segment value: '{ str_g8 }' | &&
|Segment index: '{ sy-index }'| TO itab_g2.
CATCH cx_sy_strg_par_val.
DATA(seg_nom) = |There are { sy-index - 1 } | &&
|segments in the string.|.
EXIT.
ENDTRY.
ENDDO.
"SPLIT statement specifying a colon after INTO
DATA(some_text) = `Lorem ipsum dolor sit amet, consectetur adipiscing elit`.
SPLIT some_text AT ` ` INTO: DATA(str1) DATA(str2) DATA(str3) DATA(str4), TABLE DATA(tab).
out->write( data = str_g2 name = `str_g2` ).
out->write( |\n| ).
out->write( data = str_g3 name = `str_g3` ).
out->write( |\n| ).
out->write( data = str_g4 name = `str_g4` ).
out->write( |\n| ).
out->write( data = str_g5 name = `str_g5` ).
out->write( |\n| ).
out->write( data = str_g6 name = `str_g6` ).
out->write( |\n| ).
out->write( data = itab_g1 name = `itab_g1` ).
out->write( |\n| ).
out->write( data = str_g7 name = `str_g7` ).
out->write( |\n| ).
out->write( data = str_g8 name = `str_g8` ).
out->write( |\n| ).
out->write( data = itab_g2 name = `itab_g2` ).
out->write( |\n| ).
out->write( data = seg_nom name = `seg_nom` ).
out->write( |\n| ).
out->write( data = str1 name = `str1` ).
out->write( |\n| ).
out->write( data = str2 name = `str2` ).
out->write( |\n| ).
out->write( data = str3 name = `str3` ).
out->write( |\n| ).
out->write( data = str4 name = `str4` ).
out->write( |\n| ).
out->write( data = tab name = `tab` ).
ENDMETHOD.
METHOD m09_lowercase_uppercase.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Transforming to Lower and Upper Case| ).
DATA(str_h1) = `It's a string`.
DATA(str_h2) = str_h1.
"The string functions store the result in a target variable.
DATA(str_h3) = to_upper( str_h1 ).
DATA(str_h4) = to_lower( str_h1 ).
"TRANSLATE does the transformation on the source variable.
TRANSLATE str_h1 TO UPPER CASE.
TRANSLATE str_h2 TO LOWER CASE.
"to_mixed/from_mixed functions
"sep: Specifies the separator
"case: A character-like text field. A small character specifies
"that the first character of the string is in lower case. If the
"specification is, for example, case = 'X', the first character
"is capitalized.
"min: A positive number that specifies the minimum number of
"characters that must appear before the separator. The default
"value is 1.
DATA(str_h5) = `A_GREAT_STRING`.
DATA(str_h6) = to_mixed( val = str_h5 sep = `_` ).
DATA(str_h7) = to_mixed( val = str_h5 sep = `_` case = 'x' ).
DATA(str_h8) = to_mixed( val = str_h5 sep = `_`
case = 'a' min = 3 ).
DATA(str_h9) = from_mixed( val = `someGreatString` sep = ` `
case = 'a' min = 4 ).
out->write( data = str_h3 name = `str_h3` ).
out->write( |\n| ).
out->write( data = str_h4 name = `str_h4` ).
out->write( |\n| ).
out->write( data = str_h1 name = `str_h1` ).
out->write( |\n| ).
out->write( data = str_h2 name = `str_h2` ).
out->write( |\n| ).
out->write( data = str_h6 name = `str_h6` ).
out->write( |\n| ).
out->write( data = str_h7 name = `str_h7` ).
out->write( |\n| ).
out->write( data = str_h8 name = `str_h8` ).
out->write( |\n| ).
out->write( data = str_h9 name = `str_h9` ).
ENDMETHOD.
METHOD m10_shift.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Shifting Content in Strings| ).
DATA(str_i1) = `hallo`.
DATA(str_i2) = str_i1.
DATA(str_i3) = str_i1.
DATA(str_i4) = str_i1.
"No addition; string is shifted one place to the left
SHIFT str_i2.
"Shifting string by n places; without direction,
"left by default
SHIFT str_i3 BY 2 PLACES.
"Direction explicitly specified
"Variable-length strings are extended
SHIFT str_i4 BY 3 PLACES RIGHT.
DATA(char_i1) = 'world '.
DATA(char_i2) = char_i1.
DATA(char_i3) = char_i1.
DATA(str_i5) = `world `.
"Comparison of behavior for fixed- and variable-length strings
SHIFT char_i1 BY 3 PLACES RIGHT.
SHIFT str_i5 BY 3 PLACES RIGHT.
"CIRCULAR addition: characters that are moved out of the string are
"added at the other end again
SHIFT char_i2 BY 3 PLACES RIGHT CIRCULAR.
SHIFT char_i3 BY 2 PLACES LEFT CIRCULAR.
DATA(str_i6) = ` hallo world `.
DATA(str_i7) = str_i6.
"Moving characters up to a specific character set
SHIFT str_i6 UP TO 'or'.
"Deleting leading and trailing characters with this sequence
"of statements
SHIFT str_i7 RIGHT DELETING TRAILING ` `.
SHIFT str_i7 LEFT DELETING LEADING ` `.
"String functions storing the result in a target variable
DATA(str_i8) = `some string`.
"shift_left
DATA(str_i9) = shift_left( val = str_i8 places = 3 ).
DATA(str_i10) = shift_left( val = str_i8 circular = 7 ).
"shift_right
"Note: When the parameter places is specified, the function
"shift_right has a different behavior than the SHIFT statement.
"Here, the length of the string is reduced. SHIFT extends the
"length or it remains the same.
DATA(str_i11) = shift_right( val = str_i8 places = 3 ).
DATA(str_i12) = `shift_right and trailing blanks `.
"sub: Specifying a substring; all substrings in the string that
"match the value are removed (sub also available for shift_left)
DATA(str_i13) = shift_right( val = str_i12
sub = ` and trailing blanks ` ).
DATA(str_i14) = shift_right( val = str_i12 sub = ` ` ).
DATA(str_i15) = shift_right( val = str_i12 ). "Same effect as above
out->write( |SHIFT statements:\n\n| ).
out->write( data = str_i2 name = `str_i2` ).
out->write( |\n| ).
out->write( data = str_i3 name = `str_i3` ).
out->write( |\n| ).
out->write( data = str_i4 name = `str_i4` ).
out->write( |\n| ).
out->write( data = char_i1 name = `char_i1` ).
out->write( |\n| ).
out->write( data = str_i5 name = `str_i5` ).
out->write( |\n| ).
out->write( data = char_i2 name = `char_i2` ).
out->write( |\n| ).
out->write( data = char_i3 name = `char_i3` ).
out->write( |\n| ).
out->write( data = str_i6 name = `str_i6` ).
out->write( |\n| ).
out->write( data = str_i7 name = `str_i7` ).
out->write( |\n| ).
out->write( |String functions:\n\n| ).
out->write( |\n| ).
out->write( data = str_i9 name = `str_i9` ).
out->write( |\n| ).
out->write( data = str_i10 name = `str_i10` ).
out->write( |\n| ).
out->write( data = str_i11 name = `str_i11` ).
out->write( |\n| ).
out->write( data = str_i13 name = `str_i13` ).
out->write( |\n| ).
out->write( data = str_i14 name = `str_i14` ).
out->write( |\n| ).
out->write( data = str_i15 name = `str_i15` ).
ENDMETHOD.
METHOD m11_condense.
zcl_demo_abap_aux=>set_example_divider( out = out text = |{ text }: Condensing Strings| ).
DATA(char_j1) = ' some text '.
DATA(char_j2) = ' some more text '.
DATA(char_j3) = ' a third text field literal '.
"No addition: Removes leading and trailing blanks. This is also
"true for multiple blanks. It also replaces sequences of multiple
"blanks with a single blank.
CONDENSE char_j1.
CONDENSE char_j2.
"NO-GAPS: Removes all blanks, also between words. When NO-GAPS
"is used with variable-length strings, trailing blanks remain
"removed.
CONDENSE char_j3 NO-GAPS.
"RESPECTING BLANKS: Avoiding condensing
"A use case might be the assignment of strings with fixed- to
"variable-length strings.
DATA(char_j4) = ' abcef '.
DATA(char_j5) = ' ghij '.
DATA str_j TYPE string.
"Result: ' abcef ghij '
CONCATENATE char_j4 char_j5 INTO str_j RESPECTING BLANKS.
"String function condense
"The advantage of using the string functions is
"that you can also specify random characters to be removed and
"not only blanks.
DATA(str_j1) = ` hi there `.
"No parameters specified (i. e. their default values are provided);
"works like CONDENSE statements without the NO-GAPS addition
DATA(str_j2) = condense( str_j1 ).
"Parameter 'from' specified with an initial string, 'del'/'to' not
"specified: Removes leading and trailing blanks. The 'from'
"parameter could also be specified with a text field literal:
"from = ' '
DATA(str_j3) = condense( val = str_j1 from = `` ).
"Parameter 'to' specified with an initial string, 'from'/'del' not
"specified: works like CONDENSE statements with the NO-GAPS
"addition
DATA(str_j4) = condense( val = str_j1 to = `` ).
DATA(str_j5) = `ZZseeZZZyouZZ`.
DATA(str_j6) = condense( val = str_j5 del = `Z` ).
"Parameters 'from', 'to' and 'del' are specified: Leading and
"trailing characters specified in 'del' are first removed. Then,
"in the remaining string, all substrings composed of characters
"specified in 'from' are replaced with the first character of the
"string specified in the 'to' parameter (in the example, it is a
"blank; the characters 'a', 'b', 'c' are not respected at all).
DATA(str_j7) = condense( val = str_j5
del = `Z`
from = `Z`
to = ` abc` ).
out->write( |CONDENSE statements:\n| ).
out->write( |\n| ).
out->write( data = char_j1 name = `char_j1` ).
out->write( |\n| ).
out->write( data = char_j2 name = `char_j2` ).
out->write( |\n| ).
out->write( data = char_j3 name = `char_j3` ).
out->write( |\n| ).