简体   繁体   English

',`和|和有什么区别?何时使用?

[英]What is the difference between ', ` and |, and when should they be used?

I've seen strings written like in these three ways: 我已经看过用以下三种方式编写的字符串:

lv_str  = 'test'
lv_str2 = `test`
lv_str3 = |test|

The only thing I've notice so far is that ' trims whitespaces sometimes, while ` preserves them. 到目前为止,我唯一注意到的是有时会修剪空白,而会保留空白。
I just recently found | 我最近才发现| - don't know much about it yet. -还不太了解。

Can someone explain, or post a good link here when which of these ways is used best and if there are even more ways? 何时可以最好地使用哪种方法以及是否还有更多方法,可以有人解释或在此处发布良好链接吗?

|...| | ... | denotes ABAP string templates. 表示ABAP字符串模板。 With string templates we can create a character string using texts, embedded expressions and control characters. 使用字符串模板,我们可以使用文本,嵌入的表达式和控制字符来创建字符串。

Use ' to define character-typed literals and non-integer numbers: 使用'定义字符类型的文字和非整数:

CONSTANTS some_chars TYPE char30 VALUE 'ABC'.
CONSTANTS some_number TYPE fltp VALUE '0.78'.

Use ` to define string-typed literals: 使用`定义字符串类型的文字:

CONSTANTS some_constant TYPE string VALUE `ABC`.

Use | 使用| to assemble text: 汇编文本:

DATA(message) = |Received HTTP code { status_code } with message { text }|.

This is an exhaustive list of the ways ABAP lets you define character sequences. 这是ABAP允许您定义字符序列的方式的详尽列表。

To answer the "when should they be used" part of the question: 要回答问题“何时使用它们”部分:

` and | `| are useful if trailing spaces are needed (they are ignored with ' , cf this blog post for more information, be careful SCN renders today the quotes badly so the post is confusing) : 如果需要尾随空格,则很有用(请使用'忽略空格,有关更多信息,请参阅此博客文章 ,请注意,SCN今天对引号的引用不好,因此该帖子令人困惑):

DATA(arrival) = `Hello ` && `world`.
DATA(departure) = |Good | && |bye|.

Use string templates ( | ) rather than the combination of ` and && for an easier reading (it remains very subjective, I tend to prefer | ; with my keyboard, | is easier to obtain too) : 使用字符串模板( | )而不是`&&的组合以便于阅读(它仍然很主观,我倾向于使用| ;使用键盘, |也更容易获得):

DATA(arrival) = `Dear ` && mother_name && `, thank you!`. 
DATA(departure) = |Bye { mother_name }, thank you!|.

Sometimes you don't have the choice: if a String data object is expected at a given position then you must use ` or | 有时,您别无选择:如果在给定位置需要String数据对象,则必须使用`| . There are many other cases. 还有很多其他情况。

In all other cases, I prefer to use ' (probably because I obtain it even more easily with my keyboard than | ). 在所有其他情况下,我更喜欢使用' (可能是因为我的键盘比|更容易获得它)。

Although the other answers are helpful they do not mention the most important difference between ' and ` . 尽管其他答案很有帮助,但它们并未提及'`之间最重要的区别。

A character chain defined with a single quote will be defined as type C with exactly the length of the chain even including white spaces at the beginning and the end of the character sequence. 用单引号定义的字符链将被定义为C型,其链长完全相同,甚至在字符序列的开头和结尾都包含空格。

So this one 'TEST' will get exactly the type C LENGTH 4 . 因此,此'TEST'将准确获得类型C LENGTH 4

wherever such a construct `TEST` will evaluate always to type string . 无论在哪里,这样的结构`TEST`将始终求值以键入string

This is very important for example in such a case. 例如,在这种情况下,这非常重要。

REPORT zutest3.

DATA i TYPE i VALUE 2.
DATA(l_test1) = COND #( WHEN i = 1 THEN 'ACT3' ELSE 'ACTA4').
DATA(l_test2) = COND #( WHEN i = 1 THEN `ACT3` ELSE `ACTA4`).

WRITE l_test1.
WRITE l_test2.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM