openlang

Escaping special characters inside a Delphi string literal

A Delphi string is surrounded by single quotes, such as 'some legal text'. How can a literal string contain the ' character? How can it represent a null byte (the Unicode character U+0000)?

The conversation continues 3

Follow the reasoning
  1. Permalink

    A string can contain a single quote by doubling the ' character, for example

    messageText := '''sample text''';
    Writeln(messageText)

    Here the first single quote opens the string, followed by a pair representing one quote. The ending uses the same arrangement.

    Other escape characters can be written with # and a number, for instance

    For a line break:

    messageText := 'Line break' + #13 + #10

    or simply

    messageText := 'Line break'#13#10

    Naturally, the platform-specific newline constant is the preferable choice.

  2. Permalink

    Write the ' character twice. For instance: 'Isn''t'. Write a null byte as #0.

  3. Permalink

    For the question's final part, the following can be used

    #$0000

    To insert U+0000

    Other Unicode characters can be inserted this way as well. (Make sure your font supports displaying the characters involved.)

Other sides of the same question

  1. Escaping percent characters when calling String.Format?

    My strings.xml file holds a SQL query, and I'm trying to construct the finished string in code with String.Format. The SELECT…

    Replies 3Latest activity java
  2. Formatting strings with F#

    I'm building an F# library for the first time Using string.Format gives me a complaint saying that the function doesn't exist…

    Replies 2Latest activity fsharp
  3. Extracting one section of a tcl string

    With tcl I'm attempting to break apart Reports/Report#2.rpt and keep just Report#2.rpt ; I tried this approach, but it produc…

    Replies 2Latest activity tcl