(sigil string)
(sigil string) - String Formatting and Manipulation
Provides string formatting, searching, and transformation functions. The format function uses printf-style directives for building strings, while utility functions handle common operations like trimming, splitting, and case conversion.
(import (sigil string))
(format "Hello, ~a!" "world") ; => "Hello, world!"
(string-split "a,b,c" ",") ; => ("a" "b" "c")
(string-upcase "hello") ; => "HELLO"Exports
formatprocedureBuild a formatted string using template directives.
Directives:
~a- Insert argument using display (human-readable)~s- Insert argument using write (machine-readable, quoted strings)~%- Insert a newline~~- Insert a literal tilde
(format "Name: ~a, Age: ~a" "Alice" 30)
; => "Name: Alice, Age: 30"
(format "Items: ~s" '(1 2 3))
; => "Items: (1 2 3)"
(format "Line 1~%Line 2")
; => "Line 1\nLine 2"strprocedureConcatenate values into a single string.
Each argument is converted to its string representation and concatenated. More efficient than multiple string-append calls.
(str "Hello" " " "World") ; => "Hello World"
(str "Value: " 42) ; => "Value: 42"
(str "List: " '(a b c)) ; => "List: (a b c)"string-repeatprocedureRepeat a string a given number of times.
(string-repeat "ab" 3) ; => "ababab"
(string-repeat "-" 10) ; => "----------"
(string-repeat "x" 0) ; => ""string-padprocedurePad a string to a minimum width.
By default, pads on the left with spaces. Optional arguments specify the padding character and side (left or right).
(string-pad "42" 5) ; => " 42"
(string-pad "42" 5 #\0) ; => "00042"
(string-pad "hi" 5 #\space 'right) ; => "hi "string-trimprocedureRemove whitespace from both ends of a string.
Whitespace includes spaces, tabs, and newlines.
(string-trim " hello ") ; => "hello"
(string-trim "\n\ttext\n") ; => "text"string-trim-startprocedureRemove whitespace from the beginning of a string.
(string-trim-start " hello") ; => "hello"string-trim-endprocedureRemove whitespace from the end of a string.
(string-trim-end "hello ") ; => "hello"string-findprocedureFind the position of a substring within a string.
Returns the index of the first occurrence, or #f if not found. An optional start index begins the search from that position.
(string-find "hello world" "world") ; => 6
(string-find "hello world" "x") ; => #f
(string-find "abcabc" "bc" 2) ; => 4string-indexprocedureFind the first character position matching a predicate.
Searches from left to right. Optional start and end indices limit the search range.
(string-index "hello" char-upper-case?) ; => #f
(string-index "Hello" char-upper-case?) ; => 0
(string-index "abc123" char-numeric?) ; => 3string-index-rightprocedureFind the last character position matching a predicate.
Searches from right to left. Optional start and end indices limit the search range.
(string-index-right "hello" (lambda (c) (eq? c #\l))) ; => 3
(string-index-right "abc" char-upper-case?) ; => #fstring-contains?procedureCheck if a string contains a substring.
(string-contains? "hello world" "world") ; => #t
(string-contains? "hello" "x") ; => #fstring-starts-with?procedureCheck if a string starts with a given prefix.
(string-starts-with? "hello" "he") ; => #t
(string-starts-with? "hello" "lo") ; => #f
(string-starts-with? "hello" "") ; => #tstring-ends-with?procedureCheck if a string ends with a given suffix.
(string-ends-with? "hello" "lo") ; => #t
(string-ends-with? "hello" "he") ; => #f
(string-ends-with? "hello" "") ; => #tstring-countprocedureCount non-overlapping occurrences of a substring.
Returns the number of times the substring appears in the string. Occurrences do not overlap.
(string-count "abcabc" "bc") ; => 2
(string-count "aaa" "a") ; => 3
(string-count "aaa" "aa") ; => 1 (non-overlapping)
(string-count "hello" "x") ; => 0
(string-count "" "x") ; => 0string-splitprocedureSplit a string into a list of substrings.
The delimiter is not included in the results.
(string-split "a,b,c" ",") ; => ("a" "b" "c")
(string-split "hello world" " ") ; => ("hello" "world")
(string-split "no-delim" ",") ; => ("no-delim")string-replaceprocedureReplace all occurrences of a substring.
(string-replace "hello" "l" "L") ; => "heLLo"
(string-replace "aaa" "a" "bb") ; => "bbbbbb"
(string-replace "hello" "x" "y") ; => "hello"string-upcaseprocedureConvert a string to uppercase.
(string-upcase "hello") ; => "HELLO"
(string-upcase "Hello!") ; => "HELLO!"string-downcaseprocedureConvert a string to lowercase.
(string-downcase "HELLO") ; => "hello"
(string-downcase "Hello!") ; => "hello!"string-capitalizeprocedureCapitalize the first character of a string.
The first character is converted to uppercase, and the rest of the string is left unchanged.
(string-capitalize "hello") ; => "Hello"
(string-capitalize "HELLO") ; => "HELLO"
(string-capitalize "hello world") ; => "Hello world"
(string-capitalize "") ; => ""string-titlecaseprocedureConvert a string to title case.
The first character of each word is capitalized, and all other characters are converted to lowercase. Words are separated by whitespace.
(string-titlecase "hello world") ; => "Hello World"
(string-titlecase "HELLO WORLD") ; => "Hello World"
(string-titlecase "hello") ; => "Hello"
(string-titlecase "") ; => ""string-empty?procedureCheck if a string has zero length.
(string-empty? "") ; => #t
(string-empty? "x") ; => #f
(string-empty? " ") ; => #f (use string-blank? for whitespace)string-blank?procedureCheck if a string is empty or contains only whitespace.
(string-blank? "") ; => #t
(string-blank? " ") ; => #t
(string-blank? "\n\t") ; => #t
(string-blank? " x ") ; => #fstring<?procedureCompare strings lexicographically (less than).
Characters are compared by their Unicode code points.
(string<? "apple" "banana") ; => #t
(string<? "abc" "abd") ; => #t
(string<? "abc" "abc") ; => #fstring>?procedureCompare strings lexicographically (greater than).
(string>? "banana" "apple") ; => #t
(string>? "abc" "abc") ; => #fstring<=?procedureCompare strings lexicographically (less than or equal).
(string<=? "abc" "abd") ; => #t
(string<=? "abc" "abc") ; => #t
(string<=? "abd" "abc") ; => #fstring>=?procedureCompare strings lexicographically (greater than or equal).
(string>=? "abd" "abc") ; => #t
(string>=? "abc" "abc") ; => #t
(string>=? "abc" "abd") ; => #f