The Fabl Manual
Function Index
Class Index
Globals Index
Libraries
Contents

Title Page
Introduction
Sample Code
Architecture
Syntax
Strong Typing
Polymorphism
Operators
Help
Errors
Configuration
RDF
Namespaces
Owl
Datatypes
Resources
Dot ops
Coercion
Type Casting
nil

Types
string
id
int
double
boolean
Literal
Containers
Functions
void

Home
Regarding
The Path
Classes
Delegation
Functional Values
Read/Write
Load/Store
Libraries
Imports
CGI
string

The Fabl datatype string implements xsd:string, as illustrated by:

string;
-->xsd:string

As specified by XML Schema Datatypes, a string is a sequence characters, and each character is represented by an integer. In the current Fabl implementation, one byte is allocated per character. Unicode strings should be represented using the UTF-8 encoding.

String literals in Fabl are surrounded by double quotes ("). Example:
var s = "abc";
s;
-->abc
Fabl expressions appearing within angle brackets ({}) are evaluated. For example:
var i = 23;
"twice {i} is {2*i}";
-->twice 23 is 46

Backslash (\) serves as the escape character. The escape sequences \n \t and \r denote linefeed (ascii 10), tab (ascii 9), and carriage return (ascii 13), respectively. Hex character codes are available in the form \xDD or \uDDDD where D represents a hexadecimal digit (0-F). \xDD inserts the byte with the given hex value, while \uDDDD inserts a unicode character in the UTF-8 encoding.

For characters other than n, t, r, x, and u, the character immediately following the backslash is inserted into the string as is. For example:

"A\{ \} \\.\nB \".";
->A{ } \. ->B ".

Operations on strings

The function

string plus(string x,string y)

represents concatenation. As always, the infix operator + is equivalent to plus:

"abc" + "def";
-->abcdef

The arguments to plus are not modified by this operation. The times function resembles plus, but acts by side effect: it modifies its first argument by adding the contents of its second argument to the end, and returns the first argument.

string times(string x,string y)

Here's an example:

var s = "abcd";
s * "EFG";
-->abcdEFG
s;
-->abcdEFG

If a variant of times is defined for inputs rs and x of types string and <tp>, then this variant is referred to as the "printer" for the type <tp>. Printers should add a description of x onto the end of rs. Fabl predefines printers for most of the built-in types, and Fabl programmers are free to add their own printers for the types that they introduce. Printers are exploited to generate string representations in several contexts. The most obvious is printing the values of expressions in the interactive Fabl environment. Another is computing the string representation of expressions surrounded angle brackets {} within string literals.

Here's an example of the introducction of a custom printer:

 // allocate a new class
allocate("ex:mytype",Class); 
// Here's a property
allocate("ex:description",FunctionalProperty); 
// description is a string-valued property
ex:description . rdfs:range = string;  


// Now define a printer for ex:mytype
void function times(string rs,ex:mytype x)
{ 
   rs * "[Description={x.ex:description}]";
}

var mm = new(ex:mytype);
mm.ex:description = "EXAMPLE";
mm;
-->[Description=EXAMPLE]

Sequence selection syntax (bracketing the index as in a[3]) is used for selecting and setting individual characters in a string (recall that characters are represented by ints). For example:

"abc"[2];
-->98

98 is the ascii code for "c". And,

var aa = "abc";
aa[0] = 98;
aa;
-->cac

Other operations on strings are described below. Note that these have the same meaning as their JavaScript counterparts.

int length(string s)

length(s) returns the number of characters in s

boolean equal(string x,string y)

equal(x,y) returns true if x and y contain the same sequence of characters. As always, the syntax x==y can be used instead of equal(x,y).

string addChar(string s,int n)

addChar(s,n) adds the character with code n to the end of s.

string substring(string s,int lb,int ub)

substring(s,lb,ub) returns the substring of s running from s[lb] through s[ub-1].

int indexOf(string s,string target)

indexOf(s,target) returns the index of the first occurence of the string target within s. If there is no such occurence, -1 is returned.

int indexOf(string s,string target,int fromIndex)

indexOf(s,target,fromIndex) returns the index of the first occurence of the string target within s that occurs at or beyond fromIndex.

int toInt(string s)

toInt(s) converts a string which is a sequence of digits preceded by an optional minus sign into an int. Example:

toInt("-12")+1;
-->-11

double toDouble(string s)

toDouble(s) converts a string which is a sequence of digits preceded by an optional minus sign and followed by an optional exponent into a double. Example:

toDouble("222.333e-2");
-->2.223330

The following function determines whether a string has the form of an int.

boolean isInt(string s)

Example:

isInt("34");
-->true
isInt("a34");
-->false

The behavior of the following function is analogous:

boolean isDouble(string s)

toInt never generates an error, even if isInt returns false on its argument; toInt interprets the part of the string that looks like an integer, and ignores the rest.