Raw strings
Backticks write a raw string: `C:\temp` is a string whose value is exactly the text between the backticks. A raw string has no escapes and no interpolation.
Details
\,", and${stand for themselves, so a raw string suits regular expression sources, Windows paths, and templates for other tools.- A raw string may span lines. Each line break is one
\nin the value, whatever line endings the source file uses, and the indentation of the following lines is part of the value. - A raw string cannot contain a backtick. Write that text as a string, or join raw strings with
+.
Basic examples
A backslash is a backslash:
`C:\temp` // "C:\\temp"
`C:\temp`.length() // 7Double quotes need no escape:
`say "hi"` // "say \"hi\""${ does not interpolate:
let name = "Ann"
`hello, ${name}` // "hello, \${name}"
"hello, ${name}" // "hello, Ann"Scope
Empty
Two backticks make the empty string:
`` // ""
``.isEmpty() // trueEscape sequences stay as written
\n, \t, and \u{...} are two or more characters, not an escape:
`\n`.length() // 2
"\n".length() // 1
`\u{1F600}` // "\\u{1F600}"
"\u{1F600}" // "😀"Unicode
Any character but a backtick goes in as written:
`naïve 😀`.length() // 7Several lines
A raw string spans lines. Each line break is one \n:
let poem = `roses
violets`
poem // "roses\nviolets"
poem.lines() // ["roses", "violets"]A string like any other
A raw string compares, concatenates, and calls functions as any string does:
`a\b` == "a\\b" // true
`a\b` + "/c" // "a\\b/c"
`x,y,z`.split(",") // ["x", "y", "z"]Applications
Regular expression sources
A fixed pattern reads best as a regex literal. A pattern built at run time goes to Regex.compile as a string, and its raw parts keep their backslashes single, as in a regex literal:
let n = 4
let digits = Result.unwrap(Regex.compile(`^\d{` + "${n}" + `}$`))
digits.test("2026") // true
digits.test("26") // falseA pattern full of / reads more plainly as a raw string than as a literal, which needs \/ for each one:
let origin = Result.unwrap(Regex.compile(`^https?://[^/"]+/`))
origin.find("see https://ant-lang.org/docs here")?.text // null
origin.find("https://ant-lang.org/docs")?.text // "https://ant-lang.org/"Windows paths
let dir = `C:\Users\ann\Documents`
dir.split(`\`) // ["C:", "Users", "ann", "Documents"]A raw string has no ${...}, so join it with + to put a value in. A backslash right before the closing backtick is just a backslash:
let user = "ann"
`C:\Users\` + user // "C:\\Users\\ann"Templates for other tools
A shell script keeps its own ${...} for the shell, and ends with a line break:
let script = `#!/bin/sh
echo "deploying ${APP}"
`
print(script)#!/bin/sh
echo "deploying ${APP}"JSON with quotes inside reads as written:
let text = `{"name": "Ann", "tags": ["admin"]}`
let value = Result.unwrap(Json.parse(text))
println(value)Map{ "name": "Ann", "tags": ["admin"] }Properties and relations
A raw string equals the string with each \ and " escaped, ${ written \${, and each line break written \n:
assert(`a\b "c" ${d}` == "a\\b \"c\" \${d}")
assert(`one
two` == "one\ntwo")Possible issues
A backtick ends the string
A backslash does not escape a backtick. The second backtick ends the string, and the third starts a new one that is never closed:
let quote = `it\`s` // error: unterminated raw stringJoin raw strings with a quoted backtick instead:
`it` + "`" + `s` // "it`s"Or write the text as a string, where a backtick needs no escape:
"use `ant fmt`" // "use `ant fmt`"A line break does not end a raw string either, so a missing closing backtick is the same error:
let path = `C:\temp // error: unterminated raw stringIndentation is part of the value
A multiline raw string inside a block keeps the spaces in front of each line:
func usage(): string {
`usage:
ant run <file>`
}
usage() // "usage:\n ant run <file>"See also
String literals · Regular expression literals · string · String functions · Regex.compile · Strings guide
