String

A UTF-8 character string type.

Define a string attribute type

#!test[schema]
define
  attribute name value string;

Insert data that has a string attribute

#!test[schema]
#{{
define
  entity person, owns name;
#}}
#!test[write]
insert
  $p isa person, has name "John";

Insert a standalone string attribute

#!test[write]
insert
  $_ isa 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$");

Retrieve data by string attribute

#!test[read]
match
  $p isa person, has name "John";

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

\n

Line feed

\r

Carriage return

\t

Tab

\b

Backspace

\f

Form feed

\uXXXX

The unicode character with the four-digit hex-encoded code point XXXX (see Unicode support)

\u{<code>}

The unicode character with hex-encoded code point <code>, one to six digits (see Unicode support)

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, where XXXX is 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 above FFFF.

#!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;