String Processing using ABAP string template
String Processing is one of the core components of any development. More so true when you are working with ABAP. String processing can be painful sometimes especially when you are merging or trying to embed one or more variables.
In this post, I wanted to share the usage of String templates to help process strings effortlessly.
But first,
But there is more, string templates are a powerful tool that does more than just replace string literal.
In this post, I wanted to share the usage of String templates to help process strings effortlessly.
But first,
What is a string template?
Simply Put, A string template is enclosed in two "|" (Pipe symbol) characters.Example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATA: | |
lv_string TYPE string, | |
lv_string_template TYPE string. | |
lv_string = `This is a string created using string literals`. | |
lv_string_template = |This is a string created using string templates|. | |
One of the biggest benefits of String templates is the elimination of helper variables.
A string template has three components:
Ex:
All of the control characters can be displayed by using the escape character "\" if needed.
A string template has three components:
Literal Text:
Represents its exact character string. meaning what you see is what you get.Ex:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lv_String = | This is a literal text|. |
Embedded Expressions:
An embedded expression is defined inside an opening and a closing curly bracket { ... } within string templates and can hold:
Data objects like variables/constants,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
report syst-repid. | |
DATA: | |
lv_String TYPE string. | |
START-OF-SELECTION. | |
lv_String = |The logged on user is: { syst-uname }|. | |
cl_demo_text=>show_string( lv_String ). |
Calculation expressions like: 1 + 1,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
report syst-repid. | |
DATA: | |
lv_String TYPE string. | |
START-OF-SELECTION. | |
lv_String = |5 multiplied by 5 is : { 5 * 5 }|. | |
cl_demo_text=>show_string( lv_String ). |
Predefined functions like LINES/TO_UPPER/STRLEN,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
report syst-repid. | |
DATA: | |
lv_String TYPE string. | |
START-OF-SELECTION. | |
lv_String = | lowercase to { to_upper( |uppercase| ) } |. | |
cl_demo_text=>show_string( lv_String ). |
Functional methods( methods having only one returning parameter),
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
report syst-repid. | |
DATA: | |
lv_String TYPE string. | |
CLASS lcl_main DEFINITION. | |
PUBLIC SECTION. | |
CLASS-METHODS: | |
ret_String RETURNING VALUE(rv_string) TYPE string. | |
ENDCLASS. | |
CLASS lcl_main IMPLEMENTATION. | |
METHOD ret_String . | |
rv_string = |The string was returned from a functional method.|. | |
ENDMETHOD. | |
ENDCLASS. | |
START-OF-SELECTION. | |
lv_String = | class methods says: { lcl_main=>ret_String( ) } |. | |
cl_demo_text=>show_string( lv_String ). |
Method chaining: Notice how ret_string is passed to to_upper using method chaining.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
report syst-repid. | |
DATA: | |
lv_String TYPE string. | |
CLASS lcl_main DEFINITION. | |
PUBLIC SECTION. | |
CLASS-METHODS: | |
ret_String RETURNING VALUE(rv_string) TYPE string. | |
ENDCLASS. | |
CLASS lcl_main IMPLEMENTATION. | |
METHOD ret_String . | |
rv_string = |The string was returned from a functional method.|. | |
ENDMETHOD. | |
ENDCLASS. | |
START-OF-SELECTION. | |
lv_String = | class methods says: { to_upper( lcl_main=>ret_String( ) ) } |. | |
cl_demo_text=>show_string( lv_String ). |
That's not it,
These embedded expressions can be formatted using formatting options like DATE, TIME, PAD, CASE, and many more.Control Characters:
String templates interpret the character combinations \n, \r, and \t as control characters.
\n represents Line break,
\r represents a return,
\t represents tabulator or tab.
An Example:\n represents Line break,
\r represents a return,
\t represents tabulator or tab.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
report syst-repid. | |
DATA: | |
lv_String TYPE string. | |
START-OF-SELECTION. | |
lv_String = |Normal first line.\r\tonetab\tsecondtab\tthirdtab \nLast line.|. | |
cl_demo_text=>show_string( lv_String ). |
As you can see, String templates are a very powerful yet simple tool. Hope your day-to-day life becomes easier using them.
FOr more examples and code snippets,Check out the ABAP string templates examples