Clojure, like many functional programming languages, treats strings as sequences of characters. This example demonstrates how to work with strings and characters in Clojure.
To run this program, save it as strings_and_chars.clj and use the Clojure command-line tool:
In this Clojure version:
We use count to get the length of the string, which returns the number of characters.
To get the raw bytes, we use .getBytes method and format them as hexadecimal.
Clojure’s count function already returns the number of characters (equivalent to runes in Go).
We use map-indexed with vector to iterate over the string with index and character pairs.
Characters in Clojure are represented by the char type, which can be compared directly with character literals (e.g., \t or \ส).
The examine-char function demonstrates how to work with individual characters.
Note that Clojure, being a JVM language, uses Unicode code points to represent characters, similar to Java’s char type. This means that some complex Unicode characters might be represented as surrogate pairs, which is different from Go’s rune concept.