ABAP new syntax form of string template |{}|

字符串模板生成式:

data lv_str type string.

lv_str = |Hello {sy-uname} this is SAP ABAPer world| 

character_string = |This resulted in return code { sy–subrc }|.

 

More details please continue read below article.

What’s it al about? 

Several new options for the handling of characters strings were introduced to ABAP in WAS702:

  • String Templates: the option to create a character string out of literal texts, expressions, and control characters.
  • Chaining Operator: chain two character-like operands into one new character string.
  • Character String Functions: built-in functions for searching, processing and comparing strings.

I will first explain these options and give a few examples. The last paragraph explains how the use of string templates, combined with built-in functions and the use of functional methods, limits the number of needed ABAP statements.

 

Note that the ABAP Keyword documentation contains several built-in examples, related to old and new ABAP features. These examples can be executed and debugged and its code can be analysed. Moreover, the program DEMO_EXPRESSIONS offers 45 examples of character string and byte string processing.

 

String Templates

The purpose of a string template is to create a new character string out of literal texts and embedded expressions. It largely replaces the use of the WRITE TO statement, which will be explained later on.

 

A string template is defined by using the | (pipe) symbol at the beginning and end of a template.

 

DATA: character_string TYPE string.
character_string = |This is a literal text.|.

 

This example has in fact exactly the same result as:

 

character_string = `This is a literal text.`.

 

The added value of a string template becomes clear when combining literal texts with embedded expressions and control characters. Embedded expressions are defined within a string template with curly brackets { expression }.  Note that a space between bracket and expression is obligatory.

 

An expression can be a data object (variable), a functional method, a predefined function or a calculation expression. Some examples are:

 

character_string = |{ a_numeric_variable }|.

character_string = |This resulted in return code { sy–subrc }|.

character_string = |The length of text element 001 ({ text–001 }) is { strlen(text–001 ) }|.

 

Embedded expressions have a default output format, but also several formatting options, comparable to the format options of the WRITE statement. Some examples are:

 

DATA: amount_field   TYPE vbap–netwr VALUE ‘1234567.123’,
      currency_field TYPE vbap–waerk.
character_string = |{ amount_field CURRENCY = currency_field  NUMBER = USER }|.
character_string = |{ amount_field COUNTRY = ‘GB ‘ }|.

 

Two limitations for which I haven’t found a solution yet are:

1) I could not find a formatting option for Conversion Exits. Consider the example below: the WRITE….TO… calls the conversion exit KONPD that converts an internal project number to an external project id. The string template inserts the internal project number into the string.

 

DATA: project_number TYPE proj–pspnr.
SELECT SINGLE pspnr FROM proj INTO project_number .
WRITE project_number TO character_string2.

character_string = |{ project_number }|.

 

2) The following option for text-elements is not supported in an embedded expression:

 

WRITE : ‘This is a text’(001).

 

The only option to use a numbered text is in an embedded expression:

character_string = |{ text–001 }|.

 

However, using the Chaining Operator offers a simple solution for this as is demonstrated below.

 

Chaining Operator

The Chaining Operator && can be used to create one character string out of multiple other strings and string templates. The use of the chaining operator largely replaces the CONCATENATE statement.

 

In this example, a number text, a space, an existing character string and a new string template are concatenated into a new character string.

 

character_string  =  ‘Text literal’(002) && ` ` && character_string && |{ amount_field NUMBER = USER }|.

 

Built-in functions

SAP has added several new built-in functions for searching, comparing, and processing character strings.  A few of these functions already existed before release WAS702, like for example, CHARLEN( ) or STRLEN( ). Other statements like FIND, REPLACE or TRANSLATE can now be replaced by built-in functions Also new functions have been added.

 

Note that these built-in functions can be used as part of string templates or in operand positions. In the ABAP keyword documentation, the added value of these functions is described as followed:

 

The string functions enable many string processing tasks to be done in operand positions where previously separate statements and auxiliary variables were required.

 

A few examples of built-in functions are:

 

string_result = to_mixed( val = string_field sep = ` ` ).
string_result = reverse( string_field ).
distance_between_strings = distance( val1 = string_field val2 = string_result ).

 

The functions to_mixed and reverse speak for themselves. The function distance is a similarity function that calculates the minimum number of steps needed to change one string into another string, the so called Levenshtein distance. I’m still looking for an opportunity to use it in one of my customer assignments 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值