string Delphi字符串中的一个字符如何转义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/302409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does one escape characters in Delphi string
提问by Boaz
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 字符串使用单引号,例如 ' a valid string
'。如何指定'
文字字符串中的字符?如何引用空字节(Unicode 代码点U+0000
)?
回答by Jamie
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.
当然,换行使用依赖于平台的常量更好。
回答by Toon Krijthe
To answer the last part of the question, you can use
要回答问题的最后一部分,您可以使用
###代码##00
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 字符。(请务必使用可以显示这些字符的字体。)
回答by vrad
For '
character put it twice. For example: 'Don''t'
. Null byte type as #0.
对于'
字符把它放两次。例如:'Don''t'
。空字节类型为#0。