JanetDocsPlaygroundI'm feeling luckyGitHub sign in

string/format



    cfunction

    (string/format format & values)

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


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