JanetDocsSourcePlaygroundI'm feeling luckyCommunityGitHub sign in

string/format

core-api


    cfunction
    src/core/string.c on line 536, column 1

    (string/format format & values)

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

    The following conversion specifiers are supported, where the upper 
    case specifiers 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 x)
    * `V`: format with (string x)
    * `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.

    * `p`, `P`: pretty format, truncating if necessary
    * `m`, `M`: pretty format without truncating.
    * `q`, `Q`: pretty format on one line, truncating if necessary.
    * `n`, `N`: pretty format on one line without truncation.


See also:printf3 examplesSign in to add an example
Loading...
# Print out the current time in Last-Modified header-compliant 
# form. (reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified)
(def weekdayMap [ "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" ])
(def monthMap ["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"])
(def dt (os/date (os/time) true))
(string/format "%s, %02d %s %04d %02d:%02d:%02d" 
     (weekdayMap (dt :week-day)) 
     (dt :month-day)
     (monthMap (dt :month))
     (dt :year)
     (dt :hours)
     (dt :minutes)
     (dt :seconds))
yumaikasPlayground
# There is a list of formatters here: https://janet-lang.org/capi/writing-c-functions.html


(string/format "With terminal colors: %M" [:array {:key-in "struct"}]) # => "With terminal colors: (\e[33m:array\e[0m {\e[33m:key-in\e[0m \e[35m\"struct\"\e[0m})"
roobiePlayground
You can see janet datastructure values by typing:

(string/format "%q" {:a 1 :b 2}) # => {:a 1 :b 2} 
swlkrPlayground