Text Data Types
Character (Char)
UTF-16 characters, thus allowing Unicode.
Construction
A character (Char
) has a Unicode character as its value and is specified with single quotes '
:
// read query
def output = {'a'; '1'; 'α'; '文'; '👍'}
Type Relation: Char(x)
The unary relation Char(x)
checks if x
has type Char
.
// read query
def R = {'C'; "abc"; 1}
def output(x) = R(x) and Char(x)
Noteworthy Operations
char(string, i, c)
- can enumerate the charactersc
of a strings
, with their positionsi
.concat[x, y]
- works withChar
orString
values forx
andy
.
Examples
For a string s
, char[s, i]
is the i-th char in s
, starting at 1:
// read query
def output = char["abc"]
Characters can be concatenated directly to strings with concat
:
// read query
def output = concat['a', 'b'], concat['a', "bc"]
String
Strings are sequences of Rel characters (UTF-16 characters). The index of the first character is 1.
Individual characters can be accessed with the char[str, i]
relation.
Construction
String constants are enclosed in double quotes "
.
Multiline string constants can be specified by using triple double quotes ("""
),
which are also useful for including the double quote symbol inside a string.
It is also possible to specify “raw” strings by writing raw
followed by an odd number of double quotes: for example, raw" ... "
.
Raw strings allow even greater freedom in what can be written inside a string.
Furthermore, Rel features string interpolation.
See section String Constants in Rel Language Reference for more details.
// read query
def output = {"abc"; "Michael Theodore \"Mickey\" Mouse"}
This is a multiline string constant:
// read query
def output = """
a,b,c
\\\"
1,2,"three", four
"""
This is a raw string constant:
// read query
def output = raw"""
a,b,c
\"
1,2,"three", four
"""
Notice that the output of the last example is almost identical to that of the previous one. The only difference is the initial newline character (\n
).
For convenience, a multiline string constant can begin with a newline, which is ignored.
If the newline does not directly follow the initial """
, but is, for instance, preceded by a space, then neither the newline nor the first character of the multiline string will be ignored.
Type Relation: String(x)
String(x)
tests if x
is a String
:
// read query
def R = 1; 'a'; "b"; "abc"
def output(x) = R(x) and String(x)
Noteworthy Operations
char
indexes characters in a string;
substring[s, i, j]
gives the substring between indices i
and j
inclusive:
// read query
def s = "abcde"
def output:first_char = char[s, 1]
def output:substring = substring[s, 2, 4]
The string
relation converts a wide variety of other types into their string representation:
// read query
def output = string[1] ;
string[3.4] ;
string[unix_epoch] ;
string[Hour[1]] ;
string[:a]
ic string_ic { string[:a] = "a" }
string_join
.string_replace
andstring_replace_multiple
.string_trim[string]
.concat
.regex_match
.regex_compile
.num_chars
.num_bytes
.
Examples
// read query
def output = string_trim[" a b c "]
// read query
def R = {(1, "Tokyo"); (2, "Sidney")}
def output = concat["We visited ", concat[R[1], concat[" and ", concat[R[2], "."]]]]
The last example above could also have been written with string interpolation:
def R = {(1, "Tokyo"); (2, "Sidney")}
def output = "We visited %(R[1]) and %(R[2])."
Next: Time-Related Data Types