# String

A UTF-8 character string type.

## [](#_define_a_string_attribute_type)Define a string attribute type

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

## [](#_insert_data_that_has_a_string_attribute)Insert data that has a string attribute

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

## [](#_insert_a_standalone_string_attribute)Insert a standalone string attribute

```typeql
#!test[write]
insert
  $_ isa name "John";
```

## [](#_specify_valid_values_for_a_string_attribute_type)Specify valid values for a string attribute type

```typeql
#!test[schema]
define
  attribute status value string @values("draft", "review", "published", "archived");
```

## [](#_specify_valid_values_for_owning_a_string_attribute_type)Specify valid values for owning a string attribute type

```typeql
#!test[schema]
define
  entity live-article owns status @values("published", "archived");
```

## [](#_specify_range_of_valid_values_for_a_string_attribute_type)Specify range of valid values for a string attribute type

```typeql
#!test[schema]
define
  attribute grade value string @range("A".."F");
```

## [](#_specify_range_of_valid_values_for_owning_a_string_attribute_type)Specify range of valid values for owning a string attribute type

```typeql
#!test[schema]
define
  entity passing-result owns grade @range("A".."C");
```

## [](#_constrain_a_string_attribute_type_by_regex)Constrain a string attribute type by regex

```typeql
#!test[schema]
define
  attribute email value string @regex("^.*@\w+\.\w+$");
```

## [](#_constrain_ownership_of_a_string_attribute_type_by_regex)Constrain ownership of a string attribute type by regex

```typeql
#!test[schema]
define
  entity company-contact owns email @regex("^.*@typedb\.com$");
```

## [](#_retrieve_data_by_string_attribute)Retrieve data by string attribute

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

## [](#_string_operations)String operations

Strings can be concatenated using the `+` operator.

```typeql
"Hello," + " world!" == "Hello, world!"
```

Literals, string values, and string attributes can be combined:

```typeql
#!test[read, count=1]
match
  $name isa name;
  let $greeting = "Hello, " + $name + "!";
select $greeting;
```

## [](#_literal_syntax)Literal syntax

A string literal is delimited by either double quotes or single quotes. The two forms are equivalent.

```typeql
#!test[read, count=1]
match
  let $x = "TypeDB";
  let $y = 'TypeDB';
  $x == $y;
```

Within a literal, a backslash begins an escape sequence:

```typeql
#!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](#_unicode_support))

`\u{<code>}`

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

A backslash followed by any other character is not a valid escape sequence, and the query fails to parse.

## [](#_unicode_support)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`.
    

```typeql
#!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`:

```typeql
#!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.

```typeql
#!test[read, count=1]
match
  let $x = "\u0CA01234";
  let $y = "\u{0CA0}1234";
  $x == $y;
```

[Decimal](../decimal/index.md) [Date](../date/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/typeql-reference/modules/ROOT/pages/value-types/string.adoc) Edit this page on GitHub.