Strings#
Meta: this page is mostly a method reference. Group by what you do with it; don't try to be exhaustive — link to Standard library reference for the full signatures.
The examples on this page are live: they share one Dang environment, so later snippets use earlier definitions. Each result is computed and baked in by the docs build — edit a snippet and hit Run ▶ to replay the page in your browser. Blocks that show an error are supposed to fail: the build verifies the failure the same way it verifies the results.
Construction#
Write strings as literals (see Literals), join them with +, and append with +=. A double-quoted string interpolates ${...}; a single-quoted one keeps its text as written:
let name = "Ada"
let greeting = "hello, " + name
greeting += "!"
[greeting, "${name} wrote the first program", '${name} stays put']
Backticks interpolate too, but take every backslash literally, so they're the form for code, like the regular expressions below.
Conversion#
toString(value) passes strings, enum values and custom scalar values through as their bare text, and writes anything else as the Dang literal it would be written as, its strings quoted. Interpolation, List.join and print show values the same way:
[toString(42), toString(["one", "two"]), toString({{ a: 1 }}), toString("plain")]
value :: String! turns a custom scalar back into a string (see Types and nullability):
let docs = "docs/../README.md" :: Path!
(docs :: String!).toUpper
.toInt parses an integer, in base 10 unless you pass a base from 2 to 36. A cast can't do this — "42" :: Int! is a type error:
Anything that isn't an integer in that base raises, so rescue supplies a fallback (see Errors: raise and rescue):
"0x1f".toInt(base: 16) rescue -1
Inspection#
.length counts characters (Unicode code points, not bytes):
"héllo".length
.isEmpty asks whether there are none:
["".isEmpty, greeting.isEmpty]
contains, hasPrefix, and hasSuffix look for a substring:
[greeting.contains("Ada"), greeting.hasPrefix("hello"), greeting.hasSuffix("?")]
Case#
[name.toUpper, name.toLower]
Trim and pad#
trim, trimLeft, and trimRight remove any of the given characters from the ends. trimSpace removes whitespace:
["--draft--".trim("-"), "--draft--".trimLeft("-"), " draft \n".trimSpace]
trimPrefix and trimSuffix remove one exact prefix or suffix, if it's there:
["v1.2.3".trimPrefix("v"), "main.dang".trimSuffix(".dang")]
padLeft, padRight, and center add spaces up to a width, counted in characters like .length. center puts an odd extra space on the right:
Split / replace#
split cuts at each separator. limit caps the number of parts, with the last part keeping the rest, and an empty separator splits into characters:
["a,b,c".split(","), "a,b,c".split(",", limit: 2), "abc".split("")]
replace replaces every match by default. count limits how many, and an empty old inserts between characters:
["a-b-c".replace("-", "+"), "a-b-c".replace("-", "+", count: 1), "abc".replace("", ".")]
Regex#
Regex methods — containsMatch, match, matchAll, replaceMatches, rewriteMatches, splitMatches — take a Regexp! pattern. A backtick template string auto-coerces to the Regexp scalar, so a pattern is usually written `\d+` (Go regexp/syntax). Full signatures are on Standard library reference.
let version = "dang v2.1.3"
version.containsMatch(`v\d+`)
match returns the first match, or null when there is none:
version.match(`v(\d+)\.(\d+)`).captures
version.match(`beta`)
matchAll returns every match:
replaceMatches fills in $1, $2, or $name from the match's groups. ${name} works too, but a double-quoted string would interpolate it, so write that replacement in single quotes, or as \${name} in a replacement that also interpolates. rewriteMatches computes each replacement with a block:
[
"2024-01-15".replaceMatches(`(\d+)-(\d+)-(\d+)`, "$3/$2/$1"),
"2024-01-15".replaceMatches(`(?P<year>\d+)-\d+-\d+`, '${year}!'),
"hello world".rewriteMatches(`\w+`) { m => m.string.toUpper },
]
splitMatches splits at each match:
"a1b22c".splitMatches(`\d+`)
A backtick pattern can interpolate, and its backslashes still need no doubling. Interpolated text is spliced in as regex syntax, though, so a . in it matches any character. Wrap text that should match literally in Regexp.escape:
let sep = "."
["a.b", "a-b"].map { s => s.containsMatch(`^a${sep}b$`) }
["a.b", "a-b"].map { s => s.containsMatch(`^a${Regexp.escape(sep)}b$`) }
Only a literal converts to a Regexp! on its own (see Enums and scalars). A pattern held in a variable needs a cast:
let digits = `\d+`
version.containsMatch(digits)
version.containsMatch(digits :: Regexp!)
Match object#
Returned by .match, and as the elements of .matchAll; a missing match is null. It only arises from these String/Regexp APIs, so it lives here rather than as a top-level stdlib module.
let release = version.match(`v(?P<major>\d+)\.(?P<minor>\d+)`)!
release.named
[release.start, release.end]