如何在 Delphi 中的多行字符串中使用变量?

Mar*_*uis 1 delphi string-interpolation multilinestring delphi-12-athens

Delphi 12 引入了多行字符串,我试图弄清楚它是如何工作的,来自 JavaScript 背景。在那里我可以直接在多行字符串中包含变量,例如:

const myName = 'Martin';
const myString = `Hi ${myName},
Say hello to 
multi-line
strings!`;
Run Code Online (Sandbox Code Playgroud)

这将替换${myName}为变量的内容。在 Delphi 中如何使用新的'''多行字符串来实现这一点?

Rem*_*eau 6

Delphi 不支持String interpolation,而某些语言(如 Java、C# 等)则支持 String interpolation 。新的多行语法仅允许字符串文字跨越换行符,仅此而已。

要执行您想要的操作,您仍然需要使用纯字符串连接,例如:

const myName = 'Martin';
const myString = 'Hi ' + myName +
'''
,
Say hello to 
multi-line
strings!
''';
Run Code Online (Sandbox Code Playgroud)

Delphi 与字符串插值最接近的是SysUtils.Format(),例如:

const myName = 'Martin';
const myString = Format(
'''
Hi %s,
Say hello to 
multi-line
strings!
''',
[myName]);
Run Code Online (Sandbox Code Playgroud)