-
Notifications
You must be signed in to change notification settings - Fork 286
/
zcl_demo_abap_string_proc.clas.abap
2440 lines (1949 loc) · 88.2 KB
/
zcl_demo_abap_string_proc.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>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 information on <strong>getting started with the example class</strong> and the
"! <strong>disclaimer</strong> in the ABAP Doc comment of class {@link zcl_demo_abap_aux}.</p>
CLASS zcl_demo_abap_string_proc DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES: if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_demo_abap_string_proc IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( |ABAP Cheat Sheet Example: String Processing\n\n| ).
out->write( |1) Creating Strings and Assigning Values\n\n| ).
"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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `2) 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3a) String Templates (1): 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3b) 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `4) String Templates (3): Formatting Options` ) ).
"Time, date
DATA(str_d1a) = |Date: { cl_abap_context_info=>get_system_date( ) DATE = USER }|.
DATA(str_d1b) = |Time: { cl_abap_context_info=>get_system_time( ) TIME = ISO }|.
DATA(str_d1c) = |Timestamp: { utclong_current( ) TIMESTAMP = SPACE }|.
"Upper, lower case
DATA(str_d2) = |AbCdEfG|.
DATA(str_d3) = |{ str_d2 CASE = LOWER }|.
DATA(str_d4) = |{ str_d2 CASE = UPPER }|.
"Width and alignment
DATA(str_d5) = |{ 'Left' WIDTH = 20 ALIGN = LEFT }<---|.
DATA(str_d6) = |{ 'Center' WIDTH = 20 ALIGN = CENTER }<---|.
DATA(str_d7) = |{ 'Right' WIDTH = 20 ALIGN = RIGHT }<---|.
DATA(str_d8) = |{ 'Left' WIDTH = 20 ALIGN = LEFT PAD = '.' }<---|.
DATA(str_d9) = |{ 'Center' WIDTH = 20 ALIGN = CENTER PAD = '.' }<---|.
DATA(str_d10) = |{ 'Right' WIDTH = 20 ALIGN = RIGHT PAD = '.' }<---|.
"Numbers
DATA(str_d11) = |{ - 2 / 3 DECIMALS = 3 }, {
CONV decfloat34( - 2 / 3 ) DECIMALS = 3 }, {
CONV f( - 2 / 3 ) DECIMALS = 3 }|.
"Escaping \|{} in string templates
DATA(str_d14) = |\\ \| \{ \}|.
out->write( data = str_d1a name = `str_d1a` ).
out->write( |\n| ).
out->write( data = str_d1b name = `str_d1b` ).
out->write( |\n| ).
out->write( data = str_d1c name = `str_d1c` ).
out->write( |\n| ).
out->write( data = str_d3 name = `str_d3` ).
out->write( |\n| ).
out->write( data = str_d4 name = `str_d4` ).
out->write( |\n| ).
out->write( data = str_d5 name = `str_d5` ).
out->write( |\n| ).
out->write( data = str_d6 name = `str_d6` ).
out->write( |\n| ).
out->write( data = str_d7 name = `str_d7` ).
out->write( |\n| ).
out->write( data = str_d8 name = `str_d8` ).
out->write( |\n| ).
out->write( data = str_d9 name = `str_d9` ).
out->write( |\n| ).
out->write( data = str_d10 name = `str_d10` ).
out->write( |\n| ).
out->write( data = str_d11 name = `str_d11` ).
out->write( |\n| ).
out->write( data = str_d14 name = `str_d14` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `5) 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `6a) 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `6b) 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `7) 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Modifying Strings` ) ).
out->write( |8) Transforming to Lower and Upper Case\n\n| ).
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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `9) 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` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `10) 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| ).
out->write( data = str_j name = `str_j` ).
out->write( |\n| ).
out->write( zcl_demo_abap_aux=>heading( `String function condense:` ) ).
out->write( data = str_j2 name = `str_j2` ).
out->write( |\n| ).
out->write( data = str_j3 name = `str_j3` ).
out->write( |\n| ).
out->write( data = str_j4 name = `str_j4` ).
out->write( |\n| ).
out->write( data = str_j6 name = `str_j6` ).
out->write( |\n| ).
out->write( data = str_j7 name = `str_j7` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `11) Reversing Strings` ) ).
DATA(str_k) = reverse( `ollah` ).
out->write( data = str_k name = `str_k` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `12) Inserting Substrings into Strings` ) ).
DATA(str_l1) = `abcghi`.
"Inserting into specific position
DATA(str_l2) = insert( val = str_l1 sub = `def` off = 3 ).
"off is optional. If not specified (default value off = 0)
"the result is like concatenating a string with &&
DATA(str_l3) = insert( val = str_l1 sub = `def` ).
DATA(str_l4) = `def` && str_l1.
out->write( data = str_l2 name = `str_l2` ).
out->write( |\n| ).
out->write( data = str_l3 name = `str_l3` ).
out->write( |\n| ).
out->write( data = str_l4 name = `str_l4` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `13) Overlaying Content` ) ).
DATA(incl) = '==============================CP'.
DATA(cl_name) = 'CL_SOME_CLASS '.
"Addition ONLY is not specified: All blanks are replaced
OVERLAY cl_name WITH incl.
DATA(t1) = 'a.b.c.a.b.c.A'.
DATA(t2) = 'z.x.y.Z.x.y.z'.
"Addition ONLY is specified: All characters that are specified after ONLY and that
"occur in the operand are replaced. Note that this is case-sensitive.
OVERLAY t1 WITH t2 ONLY 'ab'.
out->write( data = cl_name name = `cl_name` ).
out->write( |\n| ).
out->write( data = t1 name = `t1` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `14) Processing Substrings` ) ).
DATA(str_m1) = `Lorem ipsum dolor sit amet`.
"Extracting substring starting at a specific position
"'len' not specified means the rest of the remaining characters are
"respected
DATA(str_m2) = substring( val = str_m1 off = 6 ).
"Extracting substring with a specific length
"'off' is not specified and has the default value 0.
DATA(str_m3) = substring( val = str_m1 len = 5 ).
"Specifying both off and len parameters
DATA(str_m4) = substring( val = str_m1 off = 6 len = 5 ).
"Excursion: Getting last character of a string
DATA(str_m5) = substring( val = str_m1
off = strlen( str_m1 ) - 1
len = 1 ). "t
"Offset and length specification using the + sign after a variable
"After +, it is the offset, length is specified within parentheses.
DATA(str_m6) = str_m1+0(5).
"* means respecting the rest of the remaining string
DATA(str_m7) = str_m1+12(*).
"Excursion: Write access on substrings in fixed-length strings
DATA(char_m1) = 'Lorem ipsum dolor sit amet'.
DATA(char_m2) = char_m1.
DATA(char_m3) = char_m1.
"Deleting content
CLEAR char_m2+11(*).
"Modifying string
char_m3+0(5) = 'abcde'.
"More string functions to access substrings
"Note that lots of parameters are possible (not all covered here).
DATA(str_m8) = `aa1bb2aa3bb4`.
"Extracting a substring ...
"... after a specified substring
DATA(str_m9) = substring_after( val = str_m8 sub = `aa` ).
"... after a specified substring specifying the occurence in a
"string and restricting the length
DATA(str_m10) = substring_after( val = str_m8 sub = `aa`
occ = 2 len = 4 ).
"... before a specified substring
DATA(str_m11) = substring_before( val = str_m8 sub = `b2` ).
"... from a specified substring on. It includes the substring
"specified in sub. len/off and other parameters are possible.
DATA(str_m12) = substring_from( val = str_m8 sub = `a3` ).
"... up to a specified substring. It includes the substring
"specified in sub. len/off and other parameters are possible.
"aa1bb2aa3b
DATA(str_m13) = substring_to( val = str_m8 sub = `3b` ).
out->write( data = str_m2 name = `str_m2` ).
out->write( |\n| ).
out->write( data = str_m3 name = `str_m3` ).
out->write( |\n| ).
out->write( data = str_m4 name = `str_m4` ).
out->write( |\n| ).
out->write( data = str_m5 name = `str_m5` ).
out->write( |\n| ).
out->write( data = str_m6 name = `str_m6` ).
out->write( |\n| ).
out->write( data = str_m7 name = `str_m7` ).
out->write( |\n| ).
out->write( data = char_m2 name = `char_m2` ).
out->write( |\n| ).
out->write( data = char_m3 name = `char_m3` ).
out->write( |\n| ).
out->write( data = str_m9 name = `str_m9` ).
out->write( |\n| ).
out->write( data = str_m10 name = `str_m10` ).
out->write( |\n| ).
out->write( data = str_m11 name = `str_m11` ).
out->write( |\n| ).
out->write( data = str_m12 name = `str_m12` ).
out->write( |\n| ).
out->write( data = str_m13 name = `str_m13` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Searching and Replacing in Strings` ) ).
out->write( |15) Searching Specific Characters in Strings Using Comparison Operators and String Functions\n| ).
DATA(str_n1) = `cheers`.
"CA (contains any)
"sy-fdpos contains the offset of the first found character.
"If nothing is found, sy-fdpos contains the length of the string.
"Note that position 0 stands for the very first position.
IF str_n1 CA `aeiou`.
out->write( |CA: str_n1 contains any of the characters. | &&
|The position of the first found character is { sy-fdpos }.| ).
out->write( |\n| ).
ELSE.
out->write( |CA: str_n1 does not contain any of the characters. | &&
|The length of str_n1 is { sy-fdpos }.| ).
out->write( |\n| ).
ENDIF.
"NA (contains not any)
IF str_n1 NA `xyz`.
out->write( |NA: str_n1 does not contain any of the characters.| &&
|The length of str_n1 is { sy-fdpos }.| ).
out->write( |\n| ).
ELSE.
out->write( |NA: str_n1 contains any of the characters. | &&
|The position of the first found character is { sy-fdpos }.| ).
out->write( |\n| ).
ENDIF.
"Determining if a string is exclusively composed of a certain
"character set
IF str_n1 CO `rs`.
out->write( |CO: str_n1 contains only the characters. |
&& |The length of str_n1 is { sy-fdpos }.| ).
out->write( |\n| ).
ELSE.
out->write( |CO: str_n1 does not contain only the characters. |
&& |Offset of the first character in str_n1 that is not |
&& |contained in the second operand: { sy-fdpos }.| ).
out->write( |\n| ).
ENDIF.
"Negation of CO
IF str_n1 CN `chers`.
out->write( |CN: str_n1 does not contain only the characters. |
&& |Offset of the first character in str_n1 that is |
&& |not contained in the second operand: { sy-fdpos }.| ).
out->write( |\n| ).
ELSE.
out->write( |CN: str_n1 contains only the characters. |
&& |The length of str_n1 is { sy-fdpos }.| ).
out->write( |\n| ).
ENDIF.
"String functions
DATA(str_n2) = `Pieces of cakes.`.
"find_end returns the sum of the offset of the occurrence
DATA(res_n3) = find_end( val = str_n2 sub = `of` ). "9
"find_any_of returns the offset of the occurrence of any character contained in substring
"The search is always case-sensitive.
DATA(res_n4) = find_any_of( val = str_n2 sub = `x523z4e` ).
DATA(res_n5) = find_any_of( val = str_n2 sub = `zwq85t` ).
"find_any_not_of: Negation of the one above
"The search is always case-sensitive.
DATA(res_n6) = find_any_not_of( val = str_n2 sub = `ieces` ).
DATA(res_n7) = find_any_not_of( val = str_n2 sub = `P` ).
"count returns the number of all occurrences
DATA(res_n8) = count( val = str_n2 sub = `e` ).
DATA(res_n9) = count( val = str_n2 sub = `x` ).
"count_any_of
DATA(res_n10) = count_any_of( val = str_n2 sub = `x523z4e` ).
DATA(res_n11) = count_any_of( val = str_n2 sub = `eco` ).
"count_any_not_of
DATA(res_n12) = count_any_not_of( val = str_n2 sub = `fP` ).
DATA(res_n13) = count_any_not_of( val = str_n2 sub = `Piecs ofak.` ).
out->write( data = res_n3 name = `res_n3` ).
out->write( |\n| ).
out->write( data = res_n4 name = `res_n4` ).
out->write( |\n| ).
out->write( data = res_n5 name = `res_n5` ).
out->write( |\n| ).
out->write( data = res_n6 name = `res_n6` ).
out->write( |\n| ).
out->write( data = res_n7 name = `res_n7` ).
out->write( |\n| ).
out->write( data = res_n8 name = `res_n8` ).
out->write( |\n| ).
out->write( data = res_n9 name = `res_n9` ).
out->write( |\n| ).
out->write( data = res_n10 name = `res_n10` ).
out->write( |\n| ).
out->write( data = res_n11 name = `res_n11` ).
out->write( |\n| ).
out->write( data = res_n12 name = `res_n12` ).
out->write( |\n| ).
out->write( data = res_n13 name = `res_n13` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `16) Replacing Specific Characters in Strings` ) ).
DATA(str_o1) = `___abc_def_____ghi_`.
"The replacement is done as follows: Each character specified in
"'from' is replaced by the character in 'to' that is on the same
"position, i. e. the second character in 'from' is replaced by the
"second character specified in 'to'. If there is no equivalent in
"'to', the character in 'from' is removed from the result.
"abcdefgZZ
DATA(str_o2) = translate( val = str_o1 from = `hi_` to = `ZZ` ).
"ZZZabcZdefZZZZZghiZ
DATA(str_o3) = translate( val = str_o1 from = `_` to = `ZZ` ).
"TRANSLATE statement. The value after USING is interpreted as a
"string composed of character pairs. Starting with the first pair,
"a search is performed in text for the first character in every
"pair and each occurrence is replaced with the second character of
"the pair.
"...Zbc.def.....Yhi.
TRANSLATE str_o1 USING `_.aZgY`.
out->write( data = str_o2 name = `str_o2` ).
out->write( |\n| ).
out->write( data = str_o3 name = `str_o3` ).