可能是重复问题:
如何在 Delphi 字符串中转义字符
在 Delphi 中,字符串被包含在一对单引号'
中,但我需要在我的字符串中使用'
,但是当我使用一个'
时,它会结束整个字符串的识别。
'inside string ' but this bit is outside' inside again' and the end
有没有一些符号可以去掉下一个字符的编码作用?
可能是重复问题:
如何在 Delphi 字符串中转义字符
在 Delphi 中,字符串被包含在一对单引号'
中,但我需要在我的字符串中使用'
,但是当我使用一个'
时,它会结束整个字符串的识别。
'inside string ' but this bit is outside' inside again' and the end
有没有一些符号可以去掉下一个字符的编码作用?
你需要再加一个引号来转义引号:
Writeln('I''m in your head'); //prints: I'm in your head
Writeln(''''); //prints: '
请参阅此问题。
Delphi有QuotedStr()
函数,它可以自动地在字符串周围添加引号,并对字符串中的撇号进行转义。
procedure MyForm.MyProc;
var str : string;
begin
str = QuotedStr(MyForm.Edit1);
...
end;
QuotedStr()
函数会将编辑框中的内容放在单引号内。如果编辑框中包含单引号,它们将被正确转义。
我通常使用QuotedStr
函数来修复带引号的字符串。此外,我经常发现定义常量很有用,如CRLF
和TAB
分别表示#13#10
和#9
。有时,对于引号,以类似的方式做一些事情似乎更清晰(至少对我来说是这样)。