45

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)?

Sae1962
  • 1,122
  • 15
  • 31
Boaz
  • 25,331
  • 21
  • 69
  • 77

3 Answers3

55

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.

Sae1962
  • 1,122
  • 15
  • 31
Jamie
  • 3,150
  • 2
  • 26
  • 35
  • The only issue with the #13#10 is that the compiler will generate a warning if the +'s are missing. It will compile and run fine, but with warnings. – skamradt Nov 19 '08 at 18:59
  • 3
    When did the compiler start issuing warnings about that? I know it didn't do that in Delphi 5, and I'm pretty sure it doesn't do that Delphi 2005. What warning does it print now? – Rob Kennedy Nov 19 '08 at 21:10
  • 3
    I think `QuotedStr` / `AnsiQuotedStr` are worth mentioning. Especially useful when quoting variables, e.g. `showMessage('param value is ' + QuotedStr(param))` – Alexander Malakhov Mar 22 '12 at 09:46
15

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

#$0000   

To add U+0000

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

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
10

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

bluish
  • 26,356
  • 27
  • 122
  • 180
vrad
  • 836
  • 1
  • 7
  • 16