Skip to the content
Diaspar

03 / 10

Text

Strings that count what a reader sees, and f-strings that hold any expression.

A string is text between double quotes. It is Unicode, and it is a value: no function changes a string, it gives you another one.

A string counts what a reader sees

"héllo" has five letters, and count says five. The family below is one picture on the screen, made of five code points and eighteen bytes. It counts as one.

graphemes.dias
(def word "héllo")
(def family "👨‍👩‍👧")

(defn main [args]
  ;; count gives what a reader sees.
  (print (count word) (count family))

  ;; What is underneath is there when you ask for it.
  (print (byte-count family) (count (code-points family)))

  ;; A string is a sequence of graphemes.
  (print (first word) (last word) (subs word 1 3))
  (for [letter "añb"]
    (print "letter:" letter)))
Runs in your browser

Try it: Replace the family with a flag, "🇦🇲". One again, though it is two code points.

What a reader sees as one character is called a grapheme. A string is a sequence of them: first, last, subs and for all go by graphemes, so none of them can cut an accent from its letter. There is no separate type for a character. A grapheme is a string of one.

Strings that hold expressions

An f before the quote makes an interpolated string. Each pair of braces holds an expression, and its value goes into the text.

fstrings.dias
(def name "Alvin")
(def crew #["Alvin" "Hilvar" "Alystra"])
(def price 1234.5)

(defn main [args]
  ;; Braces hold any expression.
  (print f"Hello, {name}! There are {(count crew)} of you.")

  ;; After a colon: a format, as in Python.
  (print f"{price:.2f} | {1234567:,} | {0.256:.1%} | {255:#x}")
  (print f"[{name:<10}] [{name:>10}] [{name:^10}]")

  ;; A trailing = prints the expression too, for debugging.
  (print f"{name=} {(count crew)=}")

  ;; Two braces are one brace.
  (print f"{{name}} is {name}"))
Runs in your browser

Try it: Print price with no digits after the point, in a field ten wide: f"{price:>10.0f}".

Any expression fits in the braces, a call included. After a colon comes a format, the same little language as in Python: .2f for two digits after the point, , for thousands, <10 and >10 to pad to a width. A width counts columns on the screen, so a table of names in any script lines up.

The tools

The functions for text live in the module std.string. (use std.string) at the top of a file brings the module in, and its functions are then called through its name: string.trim.

tools.dias
(use std.string)

(def line "  the city and the stars  ")

(defn main [args]
  (print (string.trim line))
  (print (string.upper (string.trim line)))
  (print (string.words line))
  (print (string.split "red,green,blue" ","))
  (print (string.replace "the city" "city" "stars"))
  (print (string.starts-with? "diaspar" "dia") (string.contains? "diaspar" "spa"))
  (print (string.pad-start "7" 3 fill: "0") (string.repeat "ab" 3))

  ;; A raw string has no escapes: what you type is what you get.
  (print r"C:\new\table")
  (print "a tab:\there, and a new line:\nthere"))
Runs in your browser

Try it: Print the words of line one a line, each in upper case, with for.

Nothing arrives in a file without being asked for. A reader of the file can find where every name comes from by looking at its top.