Strings

String Processing

String processing functions

FunctionDescription
str.ofConvert e to string
str.splitSplit string s on separators matching regex re
str.split-onSplit string s on separators matching regex re
str.joinJoin list of strings l by interposing string s
str.join-onJoin list of strings l by interposing string s
str.matchMatch string s using regex re, return list of full match then capture groups
str.match-withMatch string s using regex re, return list of full match then capture groups
str.extract(re)Use regex re (with single capture) to extract substring of s - or error
str.extract-or(re, d, s)Use regex re (with single capture) to extract substring of s - or default d
str.matchesReturn list of all matches in string s of regex re
str.matches-ofReturn list of all matches in string s of regex re
str.matches?(re, s)Return true if re matches full string s
str.suffix(b, a)Return string b suffixed onto a
str.prefix(b, a)Return string b prefixed onto a
str.lettersReturn individual letters of s as list of strings
str.lenReturn length of string in characters
str.fmtFormat x using printf-style format spec
str.to-upperConvert string s to upper case
str.to-lowerConvert string s to lower case
str.lt(a, b)True if string a is lexicographically less than b
str.gt(a, b)True if string a is lexicographically greater than b
str.lte(a, b)True if string a is lexicographically less than or equal to b
str.gte(a, b)True if string a is lexicographically greater than or equal to b
str.base64-encodeEncode string s as base64
str.base64-decodeDecode base64 string s back to its original string
str.sha256Return the SHA-256 hash of string s as lowercase hex
str.replace(pattern, replacement, s)Replace all occurrences of regex pattern with replacement in string s. Pipeline: s str.replace(pat, rep)
str.contains?(pattern, s)True if string s contains a match for regex pattern. Pipeline: s str.contains?(pat)
str.trimTrim leading and trailing whitespace from string s
str.starts-with?(re, s)True if string s starts with a match for regex re
str.ends-with?(re, s)True if string s ends with a match for regex re
str.shell-escape(s)Wrap string in single quotes for safe shell use. Embedded single quotes are escaped
str.dq-escape(s)Escape string for use inside double quotes in shell commands. Escapes backslash, dollar, backtick, and double quote

Character Constants

We don't support the usual string escapes so these make it a little more convenient to use interpolation instead..

FunctionDescription
ch.n
ch.t
ch.dq

The ch namespace provides special characters:

  • ch.n -- Newline
  • ch.t -- Tab
  • ch.dq -- Double quote

Encoding and Hashing Examples

encoded: "hello" str.base64-encode    # "aGVsbG8="
decoded: "aGVsbG8=" str.base64-decode # "hello"
hash: "hello" str.sha256              # "2cf24dba5fb0a30e..."