Skip to main content

String

rascal-0.34.0

Synopsis

String values.

Syntax

"StringChar₁StringChar₂..." where `StringCharᵢ may be one of the following:

  • Ordinary character: Any Unicode 24-bit character except <, >, ", ' or \, a, b, or 🍕.
  • Escaped character:
    • Backslash \ followed by any of <, >, ", ' or \ represents the escaped character itself. Such as \< and \"
    • \n: newline
    • \t: tab
    • \r: carriage return
    • \b: backspace
    • \f: vertical feed
    • \u hexDigit₁ hexDigit₂ hexDigit₃ hexDigit₄ : hexadecimal escapes with four digit indexes into UNICODE, such as \uABCD.
    • \U hexDigit₁ hexDigit₂ hexDigit₃ hexDigit₄ hexDigit₅ hexDigit₆ : hexadecimal escapes with six digit indexes into UNICODE, such as \UABCDEF.
    • \a hexDigit₁ hexDigit₂: hexadecimal escapes with 2 digit indexes into ASCII (0x0 ... 0x7F), such as \a13.
  • String Interpolation:
FormDescription
<Exp>Interpolate the value of the expression as a string
<if(Exp){> ... StringChars ... <}>Conditional inclusion of Text, where StringChars may use variables introduced in Exp
<if(Exp){> ... StringChars₁ ... <} else {> ... StringChars₂ ... <}>Conditional inclusion of either StringChars₁ or StringChars₂
<for(Exp){>... StringChars ... <}>Iterative splicing of StringChars into the result, where StringChars may use variables introduced in Exp.
<while(Exp){> ... StringChars ... <}>Iterative splicing of StringChars into the result, where StringChars may use variables introduced in Exp.
<do {>... StringChars ... <} while (Exp)>Iterative splicing of StringChars into the result, where StringChars may use variables introduced in Exp.
  • Multiline:
FormDescription
StringChars₁\n StringChars₂ Strings can be multi-line without an escape or continuation marker
StringChars₂\n ' StringChars₂A margin character ' indicates where the next line starts

Types

str

Description

The string values are represented by the type str and consist of character sequences surrounded by double quotes, e.g., "a" or "a\nlong\nstring".

Multiline: Strings may span more than one line. The margin character ' indicates which part of a line will be ignored. This is useful for indenting a multi-line string with the source code that generates it.

Interpolation: String literals support so-called string interpolation: inside string constants text between angle brackets (< and >) is first executed and then replaced by its string value. Various statements (if, for, while, do) also return a value and can be used in this way. In the interpolation variant of these statements the block or blocks that are part of the statement become arbitrary text (that may itself contain interpolations).

Auto-indent: Expressions that get interpolated in a string will be auto-indented. This means that each line that results from the evaluation of the expression is prefixed with the indentation level of the position of the expression in the current string.

The following operators are defined for Strings:

There are also library functions available for Strings.

Examples

rascal>N = 13;
int: 13
rascal>"The value of N is <N>";
str: "The value of N is 13"
---
The value of N is 13
---
rascal>"The value of N*N is <N*N>";
str: "The value of N*N is 169"
---
The value of N*N is 169
---
rascal>"The value is <(N < 10) ? 10 : N*N>";
str: "The value is 169"
---
The value is 169
---

As you can see the string value of variables and expressions is interpolated in the result as expected.


Some examples of more advances string interpolation
rascal>"N is <if(N < 10){> small <} else {> large <}>";
str: "N is large "
---
N is large
---
rascal>"N is <if(N < 10){> small <} else {> large (<N>)<}>";
str: "N is large (13)"
---
N is large (13)
---
rascal>"before <for(x<-[1..5]){>a <x> b <}>after";
str: "before a 1 b a 2 b a 3 b a 4 b after"
---
before a 1 b a 2 b a 3 b a 4 b after
---

multi-line string

rascal>import IO;
ok
rascal>println("hello
>>>>>>>this
>>>>>>> is
>>>>>>> new")
hello
this
is
new
ok

multi-line string with margin:

rascal>if (true)
>>>>>>> println("this is
>>>>>>> 'what
>>>>>>> ' margins
>>>>>>> 'are good for
>>>>>>> ");
this is
what
margins
are good for

ok

auto indent:

rascal>str genMethod(str n) = "int <n>() {
>>>>>>> ' return 0;
>>>>>>> '}";
str (str): function(|prompt:///|(0,99,<1,0>,<3,27>))
rascal>str genClass() = "class myClass {
>>>>>>> ' <genMethod("myMethod")>
>>>>>>> '}";
str (): function(|prompt:///|(0,99,<1,0>,<3,21>))
rascal>println(genClass());
class myClass {
int myMethod() {
return 0;
}
}
ok

Benefits

String interpolation enables very flexible template-based text generation as used in generators for source code, markup and the like.