You can use escape codes in String literals in OCL when you need to represent a line break, tab, or quote character as part of the text.
Escape codes in OCL
An escape code is a backslash (\) followed by a character. OCL interprets the pair as a character that would otherwise be difficult to place directly in a string literal.
| Escape code | Represents | Example use |
|---|---|---|
\r\n
|
A carriage return followed by a line feed (CR+LF), commonly used as a row or line break. | Build text that is displayed on separate lines. |
\t
|
A tab character. | Separate text with tab spacing. |
\'
|
A single quote character. | Include a quoted string value when you construct SQL text. |
Insert a line break
Use \r\n where the resulting string must contain a CR+LF line break.
'First row\r\nSecond row'The resulting text has two rows:
First row
Second row
For example, use this pattern when a derived attribute or expression assembles a multi-line text value:
'Name: ' + self.Name + '\r\n' + 'Code: ' + self.CodeInsert a tab
Use \t to place a tab character between values.
self.Name + '\t' + self.CodeInclude a single quote in constructed SQL
When you build SQL text for sqlpassthroughobjects, string values in the SQL must be quoted. Use \' to include the single quotes in the OCL string.
Kundfaktura.sqlpassthroughobjects(
'select FAKTURA_ID from tbl_FAKTUROR where Anvandarnamn = \' + self.Anvandarnamn + '\''
)->firstIn this example, \' produces the opening and closing single quotes around the value of self.Anvandarnamn in the generated SQL. See Documentation:OCLOperators sqlpassthroughobjects for the full SQL passthrough patterns and constraints.
Escape codes, Unicode, and encoding
Escape codes insert specified characters into an OCL string. They are not a method for converting text between character encodings. For Unicode concepts, see Documentation:OCLOperators Unicode. For conversion between strings and byte arrays, see Documentation:Encoding.
