String Operations
Weir provides built-in string operations for construction, inspection, and transformation.
Creating Strings
Section titled “Creating Strings”String Literals
Section titled “String Literals”"hello, world""line one\nline two""" ;; empty stringConcatenation with str
Section titled “Concatenation with str”str is variadic — it concatenates any number of values into a string, converting each via its Show representation:
(str "Hello, " name "!") ;; => "Hello, Alice!"(str "x=" x " y=" y) ;; => "x=5 y=10"(str "score: " (+ base bonus)) ;; => "score: 42"Inspection
Section titled “Inspection”string-length
Section titled “string-length”Returns the length of a string:
(string-length "hello") ;; => 5(string-length "") ;; => 0(string-length "emoji: hi") ;; => 9string-ref
Section titled “string-ref”Returns the character code (integer) at a given index:
(string-ref "hello" 0) ;; => 104 (ASCII 'h')(string-ref "ABC" 1) ;; => 66 (ASCII 'B')string-contains
Section titled “string-contains”Checks if a string contains a substring:
(string-contains "hello world" "world") ;; => true(string-contains "hello world" "xyz") ;; => false(string-contains "hello" "") ;; => trueThe generic len function also works on strings:
(len "hello") ;; => 5Extraction
Section titled “Extraction”substring
Section titled “substring”Extracts a substring from start index (inclusive) to end index (exclusive):
(substring "hello world" 0 5) ;; => "hello"(substring "hello world" 6 11) ;; => "world"(substring "abcdef" 2 4) ;; => "cd"Transformation
Section titled “Transformation”string-upcase
Section titled “string-upcase”Converts all characters to uppercase:
(string-upcase "hello") ;; => "HELLO"(string-upcase "Hello World") ;; => "HELLO WORLD"string-downcase
Section titled “string-downcase”Converts all characters to lowercase:
(string-downcase "HELLO") ;; => "hello"(string-downcase "Hello World") ;; => "hello world"string-trim
Section titled “string-trim”Removes leading and trailing whitespace:
(string-trim " hello ") ;; => "hello"(string-trim "\n spaced \t") ;; => "spaced"Conversion
Section titled “Conversion”char-to-string
Section titled “char-to-string”Converts a character code (integer) to a single-character string:
(char-to-string 65) ;; => "A"(char-to-string 104) ;; => "h"(char-to-string 10) ;; => "\n"Summary
Section titled “Summary”| Function | Signature | Description |
|---|---|---|
str | (Fn ['a ...] String) | Concatenate values into a string |
string-length | (Fn [String] i64) | Length of string |
substring | (Fn [String i64 i64] String) | Extract substring (start, end) |
string-ref | (Fn [String i64] i64) | Character code at index |
string-contains | (Fn [String String] Bool) | Substring search |
string-upcase | (Fn [String] String) | To uppercase |
string-downcase | (Fn [String] String) | To lowercase |
string-trim | (Fn [String] String) | Trim whitespace |
char-to-string | (Fn [i64] String) | Char code to string |