Janet 1.43.0-e4b5045 Documentation
(Other Versions: 1.42.0 1.41.2 1.41.1 1.40.1 1.40.0 1.39.1 1.38.0 1.37.1 1.36.0 1.35.0 1.34.0 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

String Module

Index

string/ascii-lower string/ascii-upper string/bytes string/check-set string/find string/find-all string/format string/from-bytes string/has-prefix? string/has-suffix? string/join string/repeat string/replace string/replace-all string/reverse string/slice string/split string/trim string/triml string/trimr


string/ascii-lower cfunction source
(string/ascii-lower str)

Returns a new string where all bytes are replaced with the lowercase version of themselves in ASCII. Does only a very simple case check, meaning no unicode support.
Community Examples

string/ascii-upper cfunction source
(string/ascii-upper str)

Returns a new string where all bytes are replaced with the uppercase version of themselves in ASCII. Does only a very simple case check, meaning no unicode support.
Community Examples

string/bytes cfunction source
(string/bytes str)

Returns a tuple of integers that are the byte values of the string.
Community Examples

string/check-set cfunction source
(string/check-set set-bytes bytes)

Checks that only bytes from `set-bytes` are contained in `bytes`. Returns true if all bytes in `bytes` appear in `set-bytes` or false if at least one byte in `bytes` does not appear in `set-bytes`. Both arguments are bytes types.
EXAMPLES
(string/check-set :abcdef 'deadbeef) # -> true

(string/check-set "acdefilop" "encyclopedia") # -> false

(string/check-set "acgt" @"gattaca") # -> true
Community Examples

string/find cfunction source
(string/find patt str &opt start)

Searches for the first instance of pattern `patt` in string `str`. Returns the index of the first character in `patt` if found, otherwise returns nil. Search begins from `start` if provided.
Community Examples

string/find-all cfunction source
(string/find-all patt str &opt start)

Searches for all instances of pattern `patt` in string `str`. Returns an array of all indices of found patterns. Overlapping instances of the pattern are counted individually, meaning a byte in `str` may contribute to multiple found patterns. Search begins from `start` if provided.
Community Examples

string/format cfunction source
(string/format fmt & args)

Similar to C's `snprintf()`, but specialized for working with Janet values. Returns a new string.

The following conversion specifiers are supported (upper case variants generate upper case output):

* `c` - ASCII character
* `d`, `i` - integer, formatted as a decimal number
* `x`, `X` - integer, formatted as a hexadecimal number
* `o` - integer, formatted as an octal number
* `f`, `F` - floating point number, formatted as a decimal number
* `e`, `E` - floating point number, formatted in scientific notation
* `g`, `G` - floating point number, formatted in its shortest form
* `a`, `A` - floating point number, formatted as a hexadecimal number
* `s` - formatted as a string, precision indicates padding and maximum length
* `t` - emit the type of the given value
* `v` - format with `describe`
* `V` - format with `string`
* `j` - format to jdn (Janet data notation)

The following conversion specifiers are used for "pretty-printing", where the upper case variants generate colored output. These specifiers can take a precision argument to specify the maximum nesting depth to print. The multiline specifiers can also take a width argument, which defaults to 80 columns.

* `p`, `P` - pretty format, truncating if needed
* `m`, `M` - pretty format without truncating
* `q`, `Q` - pretty format on one line, truncating if needed
* `n`, `N` - pretty format on one line without truncation
Community Examples

string/from-bytes cfunction source
(string/from-bytes & byte-vals)

Creates a string from integer parameters with byte values. All integers will be coerced to the range of 1 byte 0-255.
Community Examples

string/has-prefix? cfunction source
(string/has-prefix? pfx str)

Tests whether `str` starts with `pfx`.
Community Examples

string/has-suffix? cfunction source
(string/has-suffix? sfx str)

Tests whether `str` ends with `sfx`.
Community Examples

string/join cfunction source
(string/join parts &opt sep)

Joins an array of strings into one string, optionally separated by a separator string `sep`.
Community Examples

string/repeat cfunction source
(string/repeat bytes n)

Returns a string that is `n` copies of `bytes` concatenated.
Community Examples

string/replace cfunction source
(string/replace patt subst str)

Replace the first occurrence of `patt` with `subst` in the string `str`. If `subst` is a function, it will be called with `patt` only if a match is found, and should return the actual replacement text to use. Will return the new string if `patt` is found, otherwise returns `str`.
Community Examples

string/replace-all cfunction source
(string/replace-all patt subst str)

Replace all instances of `patt` with `subst` in the string `str`. Overlapping matches will not be counted, only the first match in such a span will be replaced. If `subst` is a function, it will be called with `patt` once for each match, and should return the actual replacement text to use. Will return the new string if `patt` is found, otherwise returns `str`.
Community Examples

string/reverse cfunction source
(string/reverse str)

Returns a string that is the reversed version of `str`.
Community Examples

string/slice cfunction source
(string/slice bytes &opt start end)

Returns a string based on a range of a byte sequence `bytes`. The range is specified by optional index arguments `start` and `end` defaulting to 0 and the length of `bytes` respectively. When non-negative, `start` is inclusive and `end` is exclusive, i.e. [`start`, `end`), and their values can range from 0 through the length of `bytes`. When negative, `start` is exclusive and `end` is inclusive, i.e. (`start`, `end`], and their values can range from the negative of one more than the length of `bytes` through -1. If `start` or `end` is out of range, an error is raised.
EXAMPLES
# using non-negative `start` and `end` arguments
(string/slice "abc" 0 2) # -> "ab"
(string/slice "abc" 1 3) # -> "bc"
(string/slice :abc 1 3) # -> "bc"
(string/slice 'abc 1 3) # -> "bc"
(string/slice @"abc" 1 3) # -> "bc"

# only non-negative `start` argument
(string/slice "abc" 0) # -> "abc"
(string/slice "abc" 1) # -> "bc"
(string/slice "abc" 3) # -> ""

# errors since `start` is out of bounds
(string/slice "abc" 4)

# not using `start` / `end` duplicates string
(string/slice "abc") # -> "abc"

# only negative `start` argument
(string/slice "abc" -1) # -> ""
(string/slice "abc" -2) # -> "c"
(string/slice "abc" -4) # -> "abc"

# errors since `start` is out of bounds
(string/slice "abc" -5)

# both `start` and `end` negative
(string/slice "abc" -4 -1) # -> "abc"
(string/slice "abc" -4 -3) # -> "a"
(string/slice "abc" -4 -4) # -> ""

# errors since `end` is out of bounds
(string/slice "abc" -4 -5)
Community Examples

string/split cfunction source
(string/split sep str &opt start limit)

Splits a string `str` with separator `sep` and returns an array of substrings. The substrings will not contain `sep`. If `sep` is not found, the returned array will have one element. Will start searching for `sep` at the index `start` (if provided), and return up to a maximum of `limit` results (if provided).
EXAMPLES
# substrings split by separator
(string/split "," "x,y,z") # -> @["x" "y" "z"]

# separator not found so result has one element
(string/split "!" "1 2 3") # -> @["1 2 3"]

# start searching part-way through
(string/split "," "a,e,i,o" 3) # -> @["a,e" "i" "o"]

# limit number of results
(string/split ";" "a;b;c;d;e" 0 3) # -> @["a" "b" "c;d;e"]

# separator should be non-empty
(string/split "" "word") # -> error: expected non-empty pattern

# separator can have a length > 1
(string/split "||" "a||b||c") # -> @["a" "b" "c"]
Community Examples

string/trim cfunction source
(string/trim str &opt bytes)

Trim leading and trailing whitespace from a byte sequence `str`. If argument `bytes` is provided, treat only characters in `bytes` as whitespace.
EXAMPLES
# trim leading and trailing whitespace
(string/trim "  foo ") # -> "foo"

# default whitespace includes " \t\r\n\v\f"
(string/trim "\t bar\n\r\f") # -> "bar"

# provide custom set of characters to trim
(string/trim "_.foo_bar. \n" " ._\n") # -> "foo_bar"
Community Examples

string/triml cfunction source
(string/triml str &opt bytes)

Trim leading whitespace from a byte sequence `str`. If argument `bytes` is provided, treat only characters in `bytes` as whitespace.
EXAMPLES
# trim leading/left side whitespace
(string/triml "  foo ") # -> "foo "

# default whitespace includes " \t\r\n\v\f"
(string/triml "\t bar\n\r\f") # -> "bar\n\r\f"

# provide custom set of characters to triml
(string/triml "_.foo_bar. \n" " ._\n") # -> "foo_bar. \n"
Community Examples

string/trimr cfunction source
(string/trimr str &opt bytes)

Trim trailing whitespace from a byte sequence `str`. If argument `bytes` is provided, treat only characters in `bytes` as whitespace.
EXAMPLES
# trim trailing/right side whitespace
(string/trimr "  foo ") # -> "  foo"

# default whitespace includes " \t\r\n\v\f"
(string/trimr "\t bar\n\r\f") # -> "\t bar"

# provide custom set of characters to trimr
(string/trimr "_.foo_bar. \n" " ._\n") # -> "_.foo_bar"
Community Examples