如何在Delphi字符串中转义字符

时间:2022-07-02 22:24:58

Delphi strings use single quotes, for example 'a valid string'. How does one specify the ' character within a literal string? How would one refer to the null byte (Unicode code point U+0000)?

Delphi字符串使用单引号,例如“有效字符串”。如何在文字字符串中指定'字符?如何引用空字节(Unicode代码点U + 0000)?

3 个解决方案

#1


45  

To add a single quote to a string, you include two ' marks e.g.

要向字符串添加单引号,请包含两个'标记,例如

str := '''test string''';
Writeln(str)

In the string above, you have the normal single quotation to start a string and then two for the single quote. Same goes for the end of the string.

在上面的字符串中,您可以使用正常的单引号来启动字符串,然后使用两个单引号。同样适用于字符串的结尾。

You can also use # followed by a number for other escape character e.g.
For a new line:

您也可以使用#后跟一个数字作为其他转义字符,例如对于新行:

str := 'Newline' + #13 + #10 

or just

str := 'Newline'#13#10

Of course, using the platform-dependent constant for newline is better.

当然,使用平台相关常量来换行更好。

#2


14  

To answer the last part of the question, you can use

要回答问题的最后部分,您可以使用

#$0000   

To add U+0000

添加U + 0000

This way you can add the other Unicode chars too. (Be sure to use a font that can display those characters.)

这样您也可以添加其他Unicode字符。 (务必使用可以显示这些字符的字体。)

#3


8  

For ' character put it twice. For example: 'Don''t'. Null byte type as #0.

对于'角色说两次。例如:'不'。空字节类型为#0。

#1


45  

To add a single quote to a string, you include two ' marks e.g.

要向字符串添加单引号,请包含两个'标记,例如

str := '''test string''';
Writeln(str)

In the string above, you have the normal single quotation to start a string and then two for the single quote. Same goes for the end of the string.

在上面的字符串中,您可以使用正常的单引号来启动字符串,然后使用两个单引号。同样适用于字符串的结尾。

You can also use # followed by a number for other escape character e.g.
For a new line:

您也可以使用#后跟一个数字作为其他转义字符,例如对于新行:

str := 'Newline' + #13 + #10 

or just

str := 'Newline'#13#10

Of course, using the platform-dependent constant for newline is better.

当然,使用平台相关常量来换行更好。

#2


14  

To answer the last part of the question, you can use

要回答问题的最后部分,您可以使用

#$0000   

To add U+0000

添加U + 0000

This way you can add the other Unicode chars too. (Be sure to use a font that can display those characters.)

这样您也可以添加其他Unicode字符。 (务必使用可以显示这些字符的字体。)

#3


8  

For ' character put it twice. For example: 'Don''t'. Null byte type as #0.

对于'角色说两次。例如:'不'。空字节类型为#0。