A string in Dana is simply an array of characters:
char str[] = "hello"
Dana assumes that strings are encoded in ASCII by default, where each character is one byte. The compiler allows strings to contain UTF-8 encoded characters but their constituent bytes will be accessed as individual bytes by array indexing operations. The StringUTF API can be used to wrap UTF-8 encoded char[] array content to provide multi-byte character awareness.
For the rest of this section we assume that char[] arrays only contain ASCII characters.
Strings can contain variables identified with a $ token:
char name[] = "Amma"
char str[] = "hello $name"
In this case the two strings are automatically combined based on the value of the given variable at runtime. We can also include more complex expressions such as function calls in strings, but must wrap them in parentheses:
char str[] = "hello $(ui.getName())"
If you actually want to place a literal $ in a string you'll need to escape it:
char str[] = "This item costs \$100"
Dana does not have a separate 'character' literal form, for individual characters. A single character is assigned from a string literal:
char c = "a"
...and individual characters from a string are similarly checked for equivalence using regular string literals:
if (str[5] == ".")
//do something...
In many cases it is useful to provide or return an array of strings, or pass a string to a function which expects a data instance. In these cases we wrap a string in the data.String type:
data String {
char string[]
}
We then instantiate this type as in any other data type:
String s = new String("hello")
The API data.StringUtil is used for the most common forms of string query and manipulation.
To tokenise a string, we would use the StringUtil:explode() function:
String[] explode(char str[], char tokens[])
For example:
String parts[] = stringUtil.explode(myString, ".:")
This returns an array of String instances (as defined above) without the tokens that have been specified. A wide range of other string manipulation functions, including searching, subString, case changes, and so on, are available in the same API.