String
A UTF-8 character string type.
Insert data that has a string attribute
#!test[schema]
#{{
define
entity person, owns name;
#}}
#!test[write]
insert
$p isa person, has name "John";
Specify valid values for a string attribute type
#!test[schema]
define
attribute status value string @values("draft", "review", "published", "archived");
Specify valid values for owning a string attribute type
#!test[schema]
define
entity live-article owns status @values("published", "archived");
Specify range of valid values for a string attribute type
#!test[schema]
define
attribute grade value string @range("A".."F");
Specify range of valid values for owning a string attribute type
#!test[schema]
define
entity passing-result owns grade @range("A".."C");
Constrain a string attribute type by regex
#!test[schema]
define
attribute email value string @regex("^.*@\w+\.\w+$");
Constrain ownership of a string attribute type by regex
#!test[schema]
define
entity company-contact owns email @regex("^.*@typedb\.com$");
String operations
Strings can be concatenated using the + operator.
"Hello," + " world!" == "Hello, world!"
Literals, string values, and string attributes can be combined:
#!test[read, count=1]
match
$name isa name;
let $greeting = "Hello, " + $name + "!";
select $greeting;
Literal syntax
A string literal is delimited by either double quotes or single quotes. The two forms are equivalent.
#!test[read, count=1]
match
let $x = "TypeDB";
let $y = 'TypeDB';
$x == $y;
Within a literal, a backslash begins an escape sequence:
#!test[read, count=1]
match
let $x = "She said \"hello\"";
let $y = 'She said "hello"';
$x == $y;
The following escape sequences are supported:
| Sequence | Character |
|---|---|
|
Double quote |
|
Single quote |
|
Backslash |
|
Line feed |
|
Carriage return |
|
Tab |
|
Backspace |
|
Form feed |
|
The unicode character with the four-digit hex-encoded code point |
|
The unicode character with hex-encoded code point |
A backslash followed by any other character is not a valid escape sequence, and the query fails to parse.
Unicode support
TypeDB supports unicode characters in strings, as well as providing a syntax for escaping unicode characters. A unicode escape may be written in either of two forms:
-
\uXXXX, whereXXXXis a hex-encoded code point of exactly four digits; -
\u{<code>}, where<code>is a hex-encoded code point of one to six digits. This form is the only way to write code points aboveFFFF.
#!test[read, count=1]
match
let $x = "A\u{42}C \u{130ED}\u{13153}";
let $y = "ABC ð“ƒð“…“";
$x == $y;
The two forms are interchangeable for code points up to FFFF:
#!test[read, count=1]
match
let $x = "\u0CA0\u005F\u0CA0";
let $y = "\u{0CA0}\u{5F}\u{0CA0}";
$x == $y;
When using \uXXXX, any hex digits beyond the fourth are treated as literal text. In the following snippet, $x and $y are equal.
#!test[read, count=1]
match
let $x = "\u0CA01234";
let $y = "\u{0CA0}1234";
$x == $y;