-11 Vote

Delphi/Lazarus: Using Apostrophes ' in String

Question by Guest | 2017-02-26 at 12:43

I would like to use the apostrophe character within a string. Unfortunately, this is not possible either in Delphi or Lazarus. Every time I try to do so, the compiler only returns an error message of the type "Fatal: Syntax error ; expected but identifier found".

Interestingly, it is working like a charm using double quotes ". However, I would rather use a single quote respectively apostrophe. Is there any possibility?

ReplyPositiveNegative
2Best Answer2 Votes

When defining a string, the string is separated with an apostrophe on both sides, for example s := 'abc'. If you are now using an apostrophe within the string, the compiler only sees this delimiter character and things that your string ends at this place. So, the compiler complains about anything coming thereafter.

Solution: You have to double each apostrophe that is part of your string within your string. Here is an example:

s := 'ab''cd';

This string would be interpreted as ab'cd.

Alternatively, you can also use #39 instead of the apostrophe character:

s := #39'abc'#39;
s := #39+'abc'+#39;
s := 'ab'#39'cd';

This example, the first two rows would both result in the string 'abc' while the third line is ab'cd.
2017-02-26 at 21:20

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.