ABAP,Java,JavaScript里的字符串模板String Template

简介: ABAP,Java,JavaScript里的字符串模板String Template

As an ABAP you probably be very familiar with String Template.


String Template in ABAP

A string template creates a string from literal text, embedded expressions, and control characters in a string expression. The most powerful feature I like is we can insert ABAP variable inside the template by wrapper variable with “{ }”, see one example below:


image.png

In the runtime, ABAP will replace the String Template content with actual value stored in that variable. This functionality is especially useful when you need to populate string dynamically based on some ABAP variable, for example you need to create some ABAP class or function module whose signature is only known in the runtime specified by consumer.


The below highlighted string is the calculated result of String Template | DATA: _{ -arg_name } LIKE { -arg_name }.|.


image.png

String Template is heavily used on my series of my blogs where I attempt to simulate some nice feature in other languages using ABAP:

Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Simulate Mockito in ABAP


String Template in ES6

Say you have an array which contains name of several programming languages. And the requirement is to print lines of “Hello” plus language name stored in the array.

Pretty easy to implement, isn’t it?


image.png

image.png

Now try String Template in ES6. If you know how to use String Template in ABAP, it’s a piece of cake in ES6 as well:

image.png

See comparison below:

image.png

It seems that ABAP String Template wins in this round of comparison since lots of format_options are supported there.

image.png

String Template in Angular

Technically speaking it is not precise to name the below feature to be explained as “String Template” in Angular context, nevertheless I decide to continue using this name since it works as the same logic from end user perspective as String Template in ABAP and ES6 and also have the similar grammar.

Try this url in your browser:


image.png\Why? Check the source code of this hello world page, here I use a similar “String Template” as the example described in previous ES6 part: {{ name }} will be replaced with the value of Angular model “name” ( declared via directive ng-model ) in the runtime. But keep in mind, this replacement is done by Angular framework. This is the biggest difference compared with ABAP String Template and ES6 String Template, where the replacement is done by ABAP runtime and JavaScript execution engine accordingly.


image.png

The parse of “Hello {{name}}” itself is done by Angular as well:

image.png

Every time you type character in the input field, the content of String Template will react to this change and refresh itself. If you open F12 in Chrome development, you can observe the following trace when “Jerry” is typed.


image.png

If you would like to know in detail how Angular Data binding is implemented, see my blog Compare Data Binding mechanism: SAPUI5 and Angular.


String Template in React

image.png

If you would like to know more about debugging in React, see my blog How to get and debug converted source code in React.


Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Fibonacci Sequence in ES5, ES6 and ABAP

Java byte code and ABAP Load

How to write a correct program rejected by compiler: Exception handling in Java and in ABAP

An small example to learn Garbage collection in Java and in ABAP

String Template in ABAP, ES6, Angular and React

Try to access static private attribute via ABAP RTTI and Java Reflection


相关文章
|
21小时前
|
JavaScript 前端开发 Java
《手把手教你》系列技巧篇(四十)-java+ selenium自动化测试-JavaScript的调用执行-下篇(详解教程)
【5月更文挑战第4天】本文介绍了如何使用JavaScriptExecutor在自动化测试中实现元素高亮显示。通过创建并执行JS代码,可以改变元素的样式,例如设置背景色和边框,以突出显示被操作的元素。文中提供了一个Java示例,展示了如何在Selenium中使用此方法,并附有代码截图和运行效果展示。该技术有助于跟踪和理解测试过程中的元素交互。
4 0
|
1天前
|
Web App开发 JavaScript 前端开发
《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)
【5月更文挑战第3天】本文介绍了如何在Web自动化测试中使用JavaScript执行器(JavascriptExecutor)来完成Selenium API无法处理的任务。首先,需要将WebDriver转换为JavascriptExecutor对象,然后通过executeScript方法执行JavaScript代码。示例用法包括设置JS代码字符串并调用executeScript。文章提供了两个实战场景:一是当时间插件限制输入时,用JS去除元素的readonly属性;二是处理需滚动才能显示的元素,利用JS滚动页面。还给出了一个滚动到底部的代码示例,并提供了详细步骤和解释。
25 10
|
7天前
|
传感器 数据采集 网络协议
Java串口通信:从十六进制字符串到字节数组的正确转换与发送
Java串口通信:从十六进制字符串到字节数组的正确转换与发送
25 4
|
9天前
|
Java
在Java中,如何将字符串转换为浮点数?
【4月更文挑战第30天】在Java中,如何将字符串转换为浮点数?
16 0
|
10天前
|
存储 Java C语言
【Java探索之旅】数据类型与变量 浮点型,字符型,布尔型,字符串型
【Java探索之旅】数据类型与变量 浮点型,字符型,布尔型,字符串型
19 0
|
10天前
|
Java
JAVA刷题之字符串的一些个人思路
JAVA刷题之字符串的一些个人思路
|
14天前
|
缓存 安全 Java
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
9 0
|
1月前
|
前端开发 开发工具 Android开发
小技巧分享 - 使用 Visual Studio Code 查看和修改 ABAP 代码试读版
小技巧分享 - 使用 Visual Studio Code 查看和修改 ABAP 代码试读版
15 0
小技巧分享 - 使用 Visual Studio Code 查看和修改 ABAP 代码试读版
|
1月前
|
开发者 供应链 BI
SAP ABAP CALL SUBSCREEN 代码解析
SAP ABAP CALL SUBSCREEN 代码解析
51 0
|
1月前
|
BI
工具分享 - 将一个 ABAP Function Group 内所有 Function Module 按照代码行数从高到低排序并显示
工具分享 - 将一个 ABAP Function Group 内所有 Function Module 按照代码行数从高到低排序并显示
20 0
工具分享 - 将一个 ABAP Function Group 内所有 Function Module 按照代码行数从高到低排序并显示