-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathzcl_demo_abap_builtin_func.clas.abap
1708 lines (1251 loc) · 59.5 KB
/
zcl_demo_abap_builtin_func.clas.abap
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>Built-in Functions</strong><br/>ABAP cheat sheet example class</p>
"!
"! <p>The example class demonstrates built-in functions in ABAP.<br/>
"! Choose F9 in ADT to run the class.</p>
"!
"! <h2>Information</h2>
"! <p>Find information on getting started with the example class and the disclaimer in
"! the ABAP Doc comment of class {@link zcl_demo_abap_aux}.</p>
CLASS zcl_demo_abap_builtin_func DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
CLASS-METHODS class_constructor.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_demo_abap_builtin_func IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( |ABAP cheat sheet example: Built-in Functions\n\n| ).
out->write( `1) Logical Functions` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& boolc
*&---------------------------------------------------------------------*
"boolc returns an X or a blank of type string
DATA(int) = 0.
DATA(boolc1) = CONV abap_bool( boolc( int IS INITIAL ) ).
out->write( data = boolc1 name = `boolc1` ).
out->write( |\n| ).
DATA(boolc2) = |#{ boolc( int IS INITIAL ) }#|.
out->write( data = boolc2 name = `boolc2` ).
out->write( |\n| ).
DATA(boolc3) = |#{ boolc( int IS NOT INITIAL ) }#|.
out->write( data = boolc3 name = `boolc3` ).
out->write( |\n| ).
"Using the translate function to return a value other than X/blank
DATA(boolc4) = translate( val = boolc( int BETWEEN -3 AND 3 ) from = `X` to = `1` ).
out->write( data = boolc4 name = `boolc4` ).
out->write( |\n| ).
DATA(boolc5) = translate( val = boolc( int <> 0 ) from = ` ` to = `0` ).
out->write( data = boolc5 name = `boolc5` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& xsdbool
*&---------------------------------------------------------------------*
DATA(xsdb1) = xsdbool( 3 > 1 ).
out->write( data = xsdb1 name = `xsdb1` ).
out->write( |\n| ).
DATA(xsdb2) = |#{ xsdbool( 1 = 1 ) }#|.
out->write( data = xsdb2 name = `xsdb2` ).
out->write( |\n| ).
DATA(xsdb3) = |#{ xsdbool( 1 <> 1 ) }#|.
out->write( data = xsdb3 name = `xsdb3` ).
out->write( |\n| ).
"Comparison with boolc
IF boolc( 1 = 0 ) = xsdbool( 1 = 0 ).
DATA(res) = `equal`.
ELSE.
res = `not equal`.
ENDIF.
out->write( data = res name = `res` ).
out->write( |\n| ).
"Using xsdbool instead of, for example, an IF control
"structure or an expression with the COND operator
DATA(xsdb4) = xsdbool( -1 < 1 ).
out->write( data = xsdb4 name = `xsdb4` ).
out->write( |\n| ).
DATA truth_value1 TYPE abap_bool.
IF -1 < 1.
truth_value1 = abap_true.
ELSE.
truth_value1 = abap_false.
ENDIF.
out->write( data = truth_value1 name = `truth_value1` ).
out->write( |\n| ).
DATA(truth_value2) = COND #( WHEN -1 < 1 THEN abap_true ELSE abap_false ).
out->write( data = truth_value2 name = `truth_value2` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& contains, contains_any_of, contains_any_not_of
*&---------------------------------------------------------------------*
"-------------------- contains --------------------
"Specifying the minimum mandatory parameters
"Unlike most of the following examples, this one uses an IF control structure to
"visualize the truth value.
DATA cont1 TYPE abap_bool.
IF contains( val = `abdefghijklmn` sub = `ghi` ).
cont1 = abap_true.
ELSE.
cont1 = abap_false.
ENDIF.
out->write( data = cont1 name = `cont1` ).
out->write( |\n| ).
"case (abap_true is the default)
DATA(cont2) = xsdbool( contains( val = `ABCDE` start = `ab` case = abap_true ) ).
out->write( data = cont2 name = `cont2` ).
out->write( |\n| ).
DATA(cont3) = xsdbool( contains( val = `ABCDE` start = `ab` case = abap_false ) ).
out->write( data = cont3 name = `cont3` ).
out->write( |\n| ).
"end
DATA(cont4) = xsdbool( contains( val = `UVWXYZ` end = `xyz` case = abap_false ) ).
out->write( data = cont4 name = `cont4` ).
out->write( |\n| ).
"start
DATA(cont5) = xsdbool( contains( val = `123` start = `2` ) ).
out->write( data = cont5 name = `cont5` ).
out->write( |\n| ).
"off/len can also be specified individually
"Not specifying off means 0 by default
DATA(cont6) = xsdbool( contains( val = `##ab## ##cd##` sub = `cd` len = 5 ) ).
out->write( data = cont6 name = `cont6` ).
out->write( |\n| ).
DATA(cont7) = xsdbool( contains( val = `##ab## ##cd##` sub = `cd` off = 7 len = 5 ) ).
out->write( data = cont7 name = `cont7` ).
out->write( |\n| ).
"occ: False if there are more occurrences than specified for occ; i.e. in the following
"example, specifying the values 1, 2, 3 returns true
"abap_true is returned for the first 3 loop passes, abap_false for the fourth
DO 4 TIMES.
DATA(cont8) = xsdbool( contains( val = `ab#ab#ab#cd#ef#gh` sub = `ab` occ = sy-index ) ).
out->write( data = cont8 name = `cont8` ).
out->write( |\n| ).
ENDDO.
"pcre
"In the example, a blank is searched.
DATA(cont9) = xsdbool( contains( val = `Hallo world` pcre = `\s` ) ).
out->write( data = cont9 name = `cont9` ).
out->write( |\n| ).
"-------------------- contains_any_of --------------------
DATA(cont10) = xsdbool( contains_any_of( val = `abcdefg` sub = `xyza` ) ).
out->write( data = cont10 name = `cont10` ).
out->write( |\n| ).
DATA(cont11) = xsdbool( contains_any_of( val = `abcdefg` sub = `xyz` ) ).
out->write( data = cont11 name = `cont11` ).
out->write( |\n| ).
DATA(hi) = `1hallo`.
DATA(abc) = `abcdefghijklmnopqrstuvwxyz`.
DATA(cont12) = xsdbool( contains_any_of( val = hi start = abc ) ).
out->write( data = cont12 name = `cont12` ).
out->write( |\n| ).
DATA(cont13) = xsdbool( contains_any_of( val = hi end = abc ) ).
out->write( data = cont13 name = `cont13` ).
out->write( |\n| ).
"-------------------- contains_any_not_of --------------------
DATA(cont14) = xsdbool( contains_any_not_of( val = hi start = abc ) ).
out->write( data = cont14 name = `cont14` ).
out->write( |\n| ).
DATA(cont15) = xsdbool( contains_any_not_of( val = hi end = abc ) ).
out->write( data = cont15 name = `cont15` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& matches
*&---------------------------------------------------------------------*
"Checking validity of an email address
"abap_true
DATA(matches) = xsdbool( matches( val = `[email protected]`
pcre = `\w+(\.\w+)*@(\w+\.)+(\w{2,4})` ) ).
out->write( data = matches name = `matches` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& line_exists
*&---------------------------------------------------------------------*
TYPES: BEGIN OF s,
comp1 TYPE i,
comp2 TYPE c LENGTH 3,
END OF s.
DATA itab TYPE TABLE OF s WITH EMPTY KEY.
itab = VALUE #( ( comp1 = 1 comp2 = 'aaa' ) ( comp1 = 2 comp2 = 'bbb' ) ( comp1 = 3 comp2 = 'ccc' ) ).
DATA(str_tab) = VALUE string_table( ( `abc` ) ( `def` ) ( `ghi` ) ).
DATA(line_exists1) = xsdbool( line_exists( itab[ 1 ] ) ).
out->write( data = line_exists1 name = `line_exists1` ).
out->write( |\n| ).
DATA(line_exists2) = xsdbool( line_exists( itab[ 4 ] ) ).
out->write( data = line_exists2 name = `line_exists2` ).
out->write( |\n| ).
DATA(line_exists3) = xsdbool( line_exists( itab[ comp1 = 2 ] ) ).
out->write( data = line_exists3 name = `line_exists3` ).
out->write( |\n| ).
DATA(line_exists4) = xsdbool( line_exists( str_tab[ 2 ] ) ).
out->write( data = line_exists4 name = `line_exists4` ).
out->write( |\n| ).
DATA(line_exists5) = xsdbool( line_exists( str_tab[ table_line = `xxx` ] ) ).
out->write( data = line_exists5 name = `line_exists5` ).
out->write( |\n| ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `2) Numeric Functions` ) ).
*&---------------------------------------------------------------------*
*& abs, sign, ceil, floor, trunc, frac, ipow
*&---------------------------------------------------------------------*
"----------- abs: Returning the absolute value -----------
DATA(abs1) = abs( CONV decfloat34( '-4.756' ) ).
out->write( data = abs1 name = `abs1` ).
out->write( |\n| ).
DATA(abs2) = abs( -4 ).
out->write( data = abs2 name = `abs2` ).
out->write( |\n| ).
"----------- sign: Evaluating the sign -----------
DATA(sign1) = sign( -789 ).
out->write( data = sign1 name = `sign1` ).
out->write( |\n| ).
DATA(sign2) = sign( 5 - 5 ).
out->write( data = sign2 name = `sign2` ).
out->write( |\n| ).
DATA(sign3) = sign( -5 * -5 ).
out->write( data = sign3 name = `sign3` ).
out->write( |\n| ).
"----- ceil: smallest integer not less than the value specified -----
DATA(ceil1) = ceil( CONV decfloat34( '4.999' ) ).
out->write( data = ceil1 name = `ceil1` ).
out->write( |\n| ).
DATA(ceil2) = ceil( CONV decfloat34( '4.001' ) ).
out->write( data = ceil2 name = `ceil2` ).
out->write( |\n| ).
DATA(ceil3) = ceil( CONV decfloat34( '-4.999' ) ).
out->write( data = ceil3 name = `ceil3` ).
out->write( |\n| ).
DATA(ceil4) = ceil( CONV decfloat34( '-4.001' ) ).
out->write( data = ceil4 name = `ceil4` ).
out->write( |\n| ).
"----- floor: largest integer not less than the value specified -----
DATA(floor1) = floor( CONV decfloat34( '4.999' ) ).
out->write( data = floor1 name = `floor1` ).
out->write( |\n| ).
DATA(floor2) = floor( CONV decfloat34( '4.001' ) ).
out->write( data = floor2 name = `floor2` ).
out->write( |\n| ).
DATA(floor3) = floor( CONV decfloat34( '-4.999' ) ).
out->write( data = floor3 name = `floor3` ).
out->write( |\n| ).
DATA(floor4) = floor( CONV decfloat34( '-4.001' ) ).
out->write( data = floor4 name = `floor4` ).
out->write( |\n| ).
"------------- trunc: integer part -------------
DATA(trunc1) = trunc( CONV decfloat34( '4.999' ) ).
out->write( data = trunc1 name = `trunc1` ).
out->write( |\n| ).
DATA(trunc2) = trunc( CONV decfloat34( '4.001' ) ).
out->write( data = trunc2 name = `trunc2` ).
out->write( |\n| ).
DATA(trunc3) = trunc( CONV decfloat34( '-4.999' ) ).
out->write( data = trunc3 name = `trunc3` ).
out->write( |\n| ).
DATA(trunc4) = trunc( CONV decfloat34( '-4.001' ) ).
out->write( data = trunc4 name = `trunc4` ).
out->write( |\n| ).
"------------- frac: decimal places -------------
DATA(frac1) = frac( CONV decfloat34( '4.999' ) ).
out->write( data = frac1 name = `frac1` ).
out->write( |\n| ).
DATA(frac2) = frac( CONV decfloat34( '4.001' ) ).
out->write( data = frac2 name = `frac2` ).
out->write( |\n| ).
DATA(frac3) = frac( CONV decfloat34( '-4.999' ) ).
out->write( data = frac3 name = `frac3` ).
out->write( |\n| ).
DATA(frac4) = frac( CONV decfloat34( '-4.001' ) ).
out->write( data = frac4 name = `frac4` ).
out->write( |\n| ).
"------------- ipow: Calculalting the power -------------
DATA(ipow1) = ipow( base = 2 exp = 3 ).
out->write( data = ipow1 name = `ipow1` ).
out->write( |\n| ).
DATA(ipow2) = ipow( base = 10 exp = 0 ).
out->write( data = ipow2 name = `ipow2` ).
out->write( |\n| ).
"Exception is raised
TRY.
DATA(ipow3) = ipow( base = 10 exp = 100 ).
CATCH cx_sy_arithmetic_overflow INTO DATA(error).
out->write( error->get_text( ) ).
ENDTRY.
*&---------------------------------------------------------------------*
*& nmin, nmax
*&---------------------------------------------------------------------*
"A minimum of two, and a maximum of 9 arguments can be specified.
"Numeric data objects and numeric expressions are possible
DATA(nmin) = nmin( val1 = CONV decfloat34( '1.34' )
val2 = CONV decfloat34( '56.7' )
val3 = CONV decfloat34( '890.123' )
val4 = CONV decfloat34( '0.999' ) ).
out->write( data = nmin name = `nmin` ).
out->write( |\n| ).
DATA(nmax) = nmax( val1 = CONV decfloat34( '1.34' )
val2 = CONV decfloat34( '56.7' )
val3 = CONV decfloat34( '890.123' )
val4 = CONV decfloat34( '0.999' ) ).
out->write( data = nmax name = `nmax` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& acos, asin, atan, cos, sin, tan, cosh, sinh, tanh, exp, log, log10, sqrt
*&---------------------------------------------------------------------*
"Calculating the square root
DATA(sqrt1) = sqrt( CONV decfloat34( '9' ) ).
out->write( data = sqrt1 name = `sqrt1` ).
out->write( |\n| ).
DATA(sqrt2) = sqrt( CONV decfloat34( '40.96' ) ).
out->write( data = sqrt2 name = `sqrt2` ).
out->write( |\n| ).
"Calculating the logarithm to base 10
DATA(log10) = log10( CONV decfloat34( '1000' ) ).
out->write( data = log10 name = `log10` ).
out->write( |\n| ).
DATA(sine) = sin( '30' ).
out->write( data = sine name = `sine` ).
out->write( |\n| ).
DATA(cosine) = cos( '45' ).
out->write( data = cosine name = `cosine` ).
out->write( |\n| ).
DATA(tangent) = tan( '90' ).
out->write( data = tangent name = `tangent` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& round, rescale
*&---------------------------------------------------------------------*
"Rounding to decimal places
DATA(round1) = round( val = CONV decfloat34( '1.2374' ) dec = 2 ).
out->write( data = round1 name = `round1` ).
out->write( |\n| ).
DATA(round2) = round( val = CONV decfloat34( '1.2374' ) dec = 3 ).
out->write( data = round2 name = `round2` ).
out->write( |\n| ).
"Rounding to precision
DATA(round3) = round( val = CONV decfloat34( '1234567890123' ) prec = 10 ).
out->write( data = round3 name = `round3` ).
out->write( |\n| ).
DATA(round4) = round( val = CONV decfloat34( '1234' ) prec = 3 ).
out->write( data = round4 name = `round4` ).
out->write( |\n| ).
"Rescaling function
"Similar to the round function, the dec (for scaling) or prec (for precision)
"parameters must be specified. The input is rounded if required.
DATA(rescale1) = rescale( val = CONV decfloat34( '1234.56789' ) dec = 0 ).
out->write( data = rescale1 name = `rescale1` ).
out->write( |\n| ).
DATA(rescale2) = rescale( val = CONV decfloat34( '1234.56789' ) dec = 1 ).
out->write( data = rescale2 name = `rescale2` ).
out->write( |\n| ).
DATA(rescale3) = rescale( val = CONV decfloat34( '1234.56789' ) prec = 3 ).
out->write( data = rescale3 name = `rescale3` ).
out->write( |\n| ).
DATA(rescale4) = rescale( val = CONV decfloat34( '1234.56789' ) prec = 10 ).
out->write( data = rescale4 name = `rescale4` ).
out->write( |\n| ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3) String Functions` ) ).
*&---------------------------------------------------------------------*
*& numofchar, strlen, xstrlen
*&---------------------------------------------------------------------*
"numofchar: Trailing blanks are not counted in both strings of fixed and variable length
"strlen: Trailing blanks are not counted in strings of fixed length; in strings of
" variable length, they are counted
DATA(numofchar1) = numofchar( 'abc ' ).
out->write( data = numofchar1 name = `numofchar1` ).
out->write( |\n| ).
DATA(numofchar2) = numofchar( `abc ` ).
out->write( data = numofchar2 name = `numofchar2` ).
out->write( |\n| ).
DATA(strlen1) = strlen( 'abc ' ).
out->write( data = strlen1 name = `strlen1` ).
out->write( |\n| ).
DATA(strlen2) = strlen( `abc ` ).
out->write( data = strlen2 name = `strlen2` ).
out->write( |\n| ).
"xstrlen for type xstring
DATA(xstr) = CONV xstring( `480065006C006C006F00200077006F0072006C0064002100` ).
DATA(len_xstr) = xstrlen( xstr ).
out->write( data = len_xstr name = `len_xstr` ).
out->write( |\n| ).
"xstring -> string
DATA(conv_str) = cl_abap_conv_codepage=>create_in( )->convert( xstr ).
*&---------------------------------------------------------------------*
*& cmin, cmax
*&---------------------------------------------------------------------*
DATA(cmin) = cmin( val1 = `zzzzzzz`
val2 = `zzazzzzzzzz` "smallest argument
val3 = `zzzzabc` ).
out->write( data = cmin name = `cmin` ).
out->write( |\n| ).
DATA(cmax) = cmax( val1 = `abcdef` "biggest argument
val2 = `aaghij`
val3 = `aaaaklmn`
val4 = `aaaaaaopqrs`
val5 = `aaaaaaaaaatuvwxy`
val6 = `aaaaaaaaaaaaaz` ).
out->write( data = cmax name = `cmax` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& find, find_end, find_any_of, find_any_not_of
*&---------------------------------------------------------------------*
DATA(str) = `Pieces of cakes.`.
"---------------- find ----------------
"The find function searches for the substring specified and returns the offset
DATA(find1) = find( val = str sub = `of` ).
out->write( data = find1 name = `find1` ).
out->write( |\n| ).
DATA(find2) = find( val = str sub = `x` ).
out->write( data = find2 name = `find2` ).
out->write( |\n| ).
"case
DATA(find3) = find( val = str sub = `p` case = abap_false ).
out->write( data = find3 name = `find3` ).
out->write( |\n| ).
"off/len
DATA(find4) = find( val = str sub = `ca` off = 4 len = 5 ).
out->write( data = find4 name = `find4` ).
out->write( |\n| ).
DATA(find5) = find( val = str sub = `ca` off = 4 len = 10 ).
out->write( data = find5 name = `find5` ).
out->write( |\n| ).
"occ
DATA(find6) = find( val = str sub = `es` occ = 1 ).
out->write( data = find6 name = `find6` ).
out->write( |\n| ).
DATA(find7) = find( val = str sub = `es` occ = 2 ).
out->write( data = find7 name = `find7` ).
out->write( |\n| ).
DATA(find8) = find( val = str sub = `es` occ = 3 ).
out->write( data = find8 name = `find8` ).
out->write( |\n| ).
"pcre
DATA(find9) = find( val = str pcre = `\.` ).
out->write( data = find9 name = `find9` ).
out->write( |\n| ).
"---------------- find_end ----------------
"find_end returns the sum of the offset of the occurrence plus the length of the match
DATA(find_end1) = find_end( val = str sub = `of` ).
out->write( data = find_end1 name = `find_end1` ).
out->write( |\n| ).
DATA(find_end2) = find_end( val = str pcre = `\s` ).
out->write( data = find_end2 name = `find_end2` ).
out->write( |\n| ).
"---------------- find_any_of ----------------
"find_any_of returns the offset of the occurrence of any character contained
"in a substring. The search is always case-sensitive.
DATA(find_any_of1) = find_any_of( val = str sub = `x523z4e` ).
out->write( data = find_any_of1 name = `find_any_of1` ).
out->write( |\n| ).
DATA(find_any_of2) = find_any_of( val = str sub = `zwq85t` ).
out->write( data = find_any_of2 name = `find_any_of2` ).
out->write( |\n| ).
"---------------- find_any_not_of ----------------
"find_any_not_of is the negation of find_any_of
DATA(find_any_not_of1) = find_any_not_of( val = str sub = `ieces` ).
out->write( data = find_any_not_of1 name = `find_any_not_of1` ).
out->write( |\n| ).
DATA(find_any_not_of2) = find_any_not_of( val = str sub = `P` ).
out->write( data = find_any_not_of2 name = `find_any_not_of2` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& count, count_any_of, count_any_not_of
*&---------------------------------------------------------------------*
DATA(st) = `Pieces of cakes.`.
"---------------- count ----------------
DATA(count1) = count( val = st sub = `e` ).
out->write( data = count1 name = `count1` ).
out->write( |\n| ).
DATA(count2) = count( val = st sub = `x` ).
out->write( data = count2 name = `count2` ).
out->write( |\n| ).
"case (case-sensitive by default)
DATA(count3) = count( val = st sub = `p` case = abap_false ).
out->write( data = count3 name = `count3` ).
out->write( |\n| ).
"off/len (off is 0 by default; len is the length of sting by default minus offset)
DATA(count4) = count( val = st sub = `es` off = 3 ).
out->write( data = count4 name = `count4` ).
out->write( |\n| ).
DATA(count5) = count( val = st sub = `es` off = 9 ).
out->write( data = count5 name = `count5` ).
out->write( |\n| ).
DATA(count6) = count( val = st sub = `es` off = 3 len = 12 ).
out->write( data = count6 name = `count6` ).
out->write( |\n| ).
DATA(count7) = count( val = st sub = `es` len = 5 ).
out->write( data = count7 name = `count7` ).
out->write( |\n| ).
"pcre
DATA(count8) = count( val = st pcre = `\s` ).
out->write( data = count8 name = `count8` ).
out->write( |\n| ).
DATA(count9) = count( val = st pcre = `.` ).
out->write( data = count9 name = `count9` ).
out->write( |\n| ).
"---------------- count_any_of ----------------
DATA(count_any_of1) = count_any_of( val = st sub = `x523z4e` ).
out->write( data = count_any_of1 name = `count_any_of1` ).
out->write( |\n| ).
DATA(count_any_of2) = count_any_of( val = st sub = `eco` ).
out->write( data = count_any_of2 name = `count_any_of2` ).
out->write( |\n| ).
"---------------- count_any_not_of ----------------
DATA(count_any_not_of1) = count_any_not_of( val = st sub = `fP` ).
out->write( data = count_any_not_of1 name = `count_any_not_of1` ).
out->write( |\n| ).
DATA(count_any_not_of2) = count_any_not_of( val = st sub = `Piecs ofak.` ).
out->write( data = count_any_not_of2 name = `count_any_not_of2` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& distance
*&---------------------------------------------------------------------*
DATA(str_to_check) = `abap`.
DATA(dist1) = distance( val1 = str_to_check val2 = `abap` ).
out->write( data = dist1 name = `dist1` ).
out->write( |\n| ).
DATA(dist2) = distance( val1 = str_to_check val2 = `axbap` ).
out->write( data = dist2 name = `dist2` ).
out->write( |\n| ).
DATA(dist3) = distance( val1 = str_to_check val2 = `yabyyapy` ).
out->write( data = dist3 name = `dist3` ).
out->write( |\n| ).
DATA(dist4) = distance( val1 = str_to_check val2 = `zabapzzzzzzzzzzzz` max = 5 ).
out->write( data = dist4 name = `dist4` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& repeat
*&---------------------------------------------------------------------*
"abapabapabapabapabap
DATA(repeat1) = repeat( val = `abap` occ = 5 ).
out->write( data = repeat1 name = `repeat1` ).
out->write( |\n| ).
DATA(repeat2) = |#{ repeat( val = ` ` occ = 10 ) }#|.
out->write( data = repeat2 name = `repeat2` ).
out->write( |\n| ).
"Y (initial value returned)
DATA(repeat3) = COND #( WHEN repeat( val = `a` occ = 0 ) = `` THEN `Y` ELSE `Z` ).
out->write( data = repeat3 name = `repeat3` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& condense
*&---------------------------------------------------------------------*
DATA(str_to_condense) = ` ab cd `.
"No parameters specified, i. e. their default values are provided.
"Works like CONDENSE statement without the NO-GAPS addition.
DATA(condense1) = condense( str_to_condense ).
out->write( data = condense1 name = `condense1` ).
out->write( |\n| ).
"Parameters del/to not specified. from parameter with initial string
"(could also be a text field literal: from = ' '). This way, leading and
"trailing blanks are removed.
DATA(condense2) = condense( val = str_to_condense from = `` ).
out->write( data = condense2 name = `condense2` ).
out->write( |\n| ).
"Parameter to specified with an initial string. No other parameters.
"Works like the CONDENSE statement with the NO-GAPS addition.
DATA(condense3) = condense( val = str_to_condense to = `` ).
out->write( data = condense3 name = `condense3` ).
out->write( |\n| ).
"Parameter del specifies the leading/trailing characters to be removed.
DATA(condense4) = condense( val = `##see###you##` del = `#` ).
out->write( data = condense4 name = `condense4` ).
out->write( |\n| ).
"If from and to are specified along with del, leading/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.
DATA(condense5) = condense( val = ` Rock'xxx'Roller`
del = `re `
from = `x`
to = `n` ).
out->write( data = condense5 name = `condense5` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& concat_lines_of
*&---------------------------------------------------------------------*
DATA(stringtable) = VALUE string_table( ( `a` ) ( `b` ) ( `c` ) ).
DATA(con1) = concat_lines_of( table = stringtable ).
out->write( data = con1 name = `con1` ).
out->write( |\n| ).
DATA(con2) = concat_lines_of( table = stringtable sep = ` ` ).
out->write( data = con2 name = `con2` ).
out->write( |\n| ).
DATA(con3) = concat_lines_of( table = stringtable sep = `/` ).
out->write( data = con3 name = `con3` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& reverse
*&---------------------------------------------------------------------*
DATA(reverse) = reverse( `paba` ).
out->write( data = reverse name = `reverse` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& escape
*&---------------------------------------------------------------------*
"Context: URLs
DATA(esc1) = escape( val = '...test: 5@8...'
format = cl_abap_format=>e_url_full ).
out->write( data = esc1 name = `esc1` ).
out->write( |\n| ).
"Context: JSON
DATA(esc2) = escape( val = 'some "test" json \ with backslash and double quotes'
format = cl_abap_format=>e_json_string ).
out->write( data = esc2 name = `esc2` ).
out->write( |\n| ).
"Context: String templates
DATA(esc3) = escape( val = 'Special characters in string templates: |, \, {, }'
format = cl_abap_format=>e_string_tpl ).
out->write( data = esc3 name = `esc3` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& insert
*&---------------------------------------------------------------------*
DATA(to_be_inserted) = `ABAP`.
DATA(insert1) = insert( val = to_be_inserted sub = `#` ).
out->write( data = insert1 name = `insert1` ).
out->write( |\n| ).
DATA(insert2) = insert( val = to_be_inserted sub = `#` off = 1 ).
out->write( data = insert2 name = `insert2` ).
out->write( |\n| ).
DATA(insert3) = insert( val = to_be_inserted sub = `#` off = strlen( to_be_inserted ) ).
out->write( data = insert3 name = `insert3` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& match
*&---------------------------------------------------------------------*
DATA(match1) = match( val = `The email address is [email protected].`
pcre = `\w+(\.\w+)*@(\w+\.)+(\w{2,4})` ).
out->write( data = match1 name = `match1` ).
out->write( |\n| ).
"Find blank (without inlcuding it in the result indicated by \K) and
"the following 2 characters, second occurrence
DATA(match2) = match( val = `The email address is [email protected].`
pcre = `\s\K..`
occ = 2 ).
out->write( data = match2 name = `match2` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& replace
*&---------------------------------------------------------------------*
DATA(to_be_replaced) = `Pieces of cakes.`.
DATA(replace1) = replace( val = to_be_replaced sub = `es` with = `#` ).
out->write( data = replace1 name = `replace1` ).
out->write( |\n| ).
"case
DATA(replace2) = replace( val = to_be_replaced sub = `p` case = abap_false with = `#` ).
out->write( data = replace2 name = `replace2` ).
out->write( |\n| ).
"occ
DATA(replace3) = replace( val = to_be_replaced sub = ` ` occ = 2 with = `#` ).
out->write( data = replace3 name = `replace3` ).
out->write( |\n| ).
"The value 0 in occ means respecting all occurrences.
DATA(replace4) = replace( val = to_be_replaced sub = `e` occ = 0 with = `#` ).
out->write( data = replace4 name = `replace4` ).
out->write( |\n| ).
"pcre
DATA(replace5) = replace( val = to_be_replaced pcre = `\s` with = `#` ).
out->write( data = replace5 name = `replace5` ).
out->write( |\n| ).
DATA(replace6) = replace( val = to_be_replaced pcre = `\s` occ = 2 with = `#` ).
out->write( data = replace6 name = `replace6` ).
out->write( |\n| ).
"Replacement determined by offset/length specification only (no sub/pcre specification)
DATA(replace7) = replace( val = to_be_replaced off = 5 with = `#` ).
out->write( data = replace7 name = `replace7` ).
out->write( |\n| ).
DATA(replace8) = replace( val = to_be_replaced len = 5 with = `#` ).
out->write( data = replace8 name = `replace8` ).
out->write( |\n| ).
DATA(replace9) = replace( val = to_be_replaced off = 3 len = 7 with = `#` ).
out->write( data = replace9 name = `replace9` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& segment
*&---------------------------------------------------------------------*
"index: Number of segment
"sep: Substring specified is searched and used as limit
DATA(segment1) = segment( val = `Hallo,world,123` index = 1 sep = `,` ).
out->write( data = segment1 name = `segment1` ).
out->write( |\n| ).
DATA(segment2) = segment( val = `Hallo,world,123` index = -1 sep = `,` ).
out->write( data = segment2 name = `segment2` ).
out->write( |\n| ).
DATA(segment3) = segment( val = `Hallo<br>world<br>123` index = 2 sep = `<br>` ).
out->write( data = segment3 name = `segment3` ).
out->write( |\n| ).
"space: Each individual character is searched and used as limit
DATA(to_be_segmented) = `a/b#c d.e`.
DATA(segment4) = segment( val = `a/b#c d.e` index = 2 space = `. #/` ).
out->write( data = segment4 name = `segment4` ).
out->write( |\n| ).
DATA segment_tab TYPE string_table.
DO.
TRY.
INSERT segment( val = to_be_segmented
index = sy-index
space = `. #/` ) INTO TABLE segment_tab.
CATCH cx_sy_strg_par_val.
EXIT.
ENDTRY.
ENDDO.
out->write( data = segment_tab name = `segment_tab` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& shift_left, shift_right
*&---------------------------------------------------------------------*
DATA(to_be_shifted) = ` hallo `.
"------------------- shift_left -------------------
DATA(shift_left1) = shift_left( val = to_be_shifted places = 3 ).
out->write( data = shift_left1 name = `shift_left1` ).
out->write( |\n| ).
"circular parameter: characters that are moved out of the string are
"added at the other end again
DATA(shift_left2) = shift_left( val = to_be_shifted circular = 2 ).
out->write( data = shift_left2 name = `shift_left2` ).
out->write( |\n| ).
DATA(shift_left3) = shift_left( val = to_be_shifted sub = ` hal` ).
out->write( data = shift_left3 name = `shift_left3` ).
out->write( |\n| ).
"No parameter except val: Behaves as if sub was passed a blank character
DATA(shift_left4) = shift_left( val = to_be_shifted ).
out->write( data = shift_left4 name = `shift_left4` ).
out->write( |\n| ).
DATA(shift_left5) = shift_left( val = to_be_shifted sub = ` ` ).
out->write( data = shift_left5 name = `shift_left5` ).
out->write( |\n| ).
"------------------- shift_right -------------------
DATA(shift_right1) = shift_right( val = to_be_shifted places = 3 ).
out->write( data = shift_right1 name = `shift_right1` ).
out->write( |\n| ).
DATA(shift_right2) = shift_right( val = to_be_shifted circular = 2 ).
out->write( data = shift_right2 name = `shift_right2` ).
out->write( |\n| ).
DATA(shift_right3) = shift_right( val = to_be_shifted sub = `o ` ).
out->write( data = shift_right3 name = `shift_right3` ).
out->write( |\n| ).
DATA(shift_right4) = shift_right( val = to_be_shifted ).
out->write( data = shift_right4 name = `shift_right4` ).
out->write( |\n| ).
*&---------------------------------------------------------------------*
*& substring, substring_after, substring_before, substring_to, substring_from
*&---------------------------------------------------------------------*
DATA(s4func) = `Lorem ipsum dolor sit amet`.
"------------------- substring -------------------
"Extracting substring starting at a specific position
"'len' not specified means the rest of the remaining characters is
"respected
DATA(substr1) = substring( val = s4func off = 6 ).
out->write( data = substr1 name = `substr1` ).
out->write( |\n| ).
"Extracting substring with a specific length
"'off' is not specified and has the default value 0.
DATA(substr2) = substring( val = s4func len = 5 ).
out->write( data = substr2 name = `substr2` ).
out->write( |\n| ).
"Specifying both off and len parameters
DATA(substr3) = substring( val = s4func off = 6 len = 5 ).
out->write( data = substr3 name = `substr3` ).
out->write( |\n| ).
"------------------- substring_after -------------------
"Extracting a substring ...
"... after a specified substring
DATA(substr_after1) = substring_after( val = s4func sub = `or` ).
out->write( data = substr_after1 name = `substr_after1` ).
out->write( |\n| ).
"... after a specified substring specifying the occurence in a string
"and restricting the length
"occ/case
DATA(substr_after2) = substring_after( val = s4func sub = `oR` occ = 2 len = 7 case = abap_false ).