### Install cl-str with Quicklisp Source: https://github.com/vindarel/cl-str/blob/master/README.md Instructions for installing the cl-str library using Quicklisp, including adding it to project dependencies and updating the Quicklisp distribution. ```Common Lisp (ql:quickload :str) ``` ```Common Lisp (ql:update-dist "quicklisp") ``` -------------------------------- ### Install cl-str Library Source: https://github.com/vindarel/cl-str/blob/master/README.md This snippet shows how to load the cl-str library using Quicklisp, the standard Lisp implementation dependency manager. Ensure Quicklisp is installed and configured before running this command. ```common-lisp (ql:quickload "str") ``` -------------------------------- ### Check cl-str Version Source: https://github.com/vindarel/cl-str/blob/master/README.md Shows how to check the version of the installed cl-str library. ```Common Lisp (str:version) ``` -------------------------------- ### Use Global Parameters in cl-str Source: https://github.com/vindarel/cl-str/blob/master/README.md Demonstrates the use of global parameters such as `:ignore-case` to modify the behavior of string functions. This example shows how to set the `str:*ignore-case*` parameter. ```Common Lisp (let ((str:*ignore-case* t)) (str:ends-with-p "BAR" "foobar")) ``` -------------------------------- ### Check if String Starts With Substring Source: https://github.com/vindarel/cl-str/blob/master/README.md The `starts-with-p` function verifies if a string `s` begins with a specified substring `start`. It supports case-insensitive comparison via the `:ignore-case` keyword argument. ```common-lisp (starts-with-p "foo" "foobar") ;; => T (starts-with-p "FOO" "foobar") ;; => NIL (starts-with-p "FOO" "foobar" :ignore-case t) ;; => T ``` -------------------------------- ### Get First Character of String Source: https://github.com/vindarel/cl-str/blob/master/README.md Returns the first character of a given string. If the string is empty, it returns an empty string. ```lisp (s-first "foobar") ;; => "f" (s-first "") ;; => "" ``` -------------------------------- ### Format List as Table using cl-ansi-term (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md This example demonstrates how to format a list of data into a human-readable table using the `cl-ansi-term` library. It shows how to load the library and then use the `term:table` function with specified column widths to format nested lists. ```lisp CL-USER> (ql:quickload "cl-ansi-term") CL-USER> (term:table '(("name" "age" "email") ("me" 7 "some@blah") ("me" 7 "some@with-some-longer.email")) :column-width '(10 4 20)) +---------+---+-------------------+ |name |age|email | +---------+---+-------------------+ |me |7 |some@blah | +---------+---+-------------------+ |me |7 |some@with-some-l(…)| +---------+---+-------------------+ ``` -------------------------------- ### Ensure Prefix/Suffix for Strings Source: https://github.com/vindarel/cl-str/blob/master/README.md Ensures a string starts or ends with a specified prefix or suffix. If the string already has the desired prefix/suffix, it remains unchanged. Otherwise, a new string is returned with the prefix/suffix added. ```lisp (str:ensure-prefix "/" "abc/") => "/abc/" (str:ensure-prefix "/" "/abc/") => "/abc/" ``` -------------------------------- ### Check String Prefix/Suffix Properties Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:prefixp` and `str:suffixp` functions check if a string starts or ends with any of the strings in a given list, respectively. These are useful for checking against multiple possible prefixes or suffixes. ```common-lisp (str:prefixp '("http://" "https://") url) (str:suffixp '(".html" ".htm") filename) ``` -------------------------------- ### Get Nth Character using elt (Alternative) Source: https://github.com/vindarel/cl-str/blob/master/README.md Demonstrates using the Common Lisp `elt` function to access a character at a specific index, and how to convert it to a string. ```lisp (elt "test" 1) ;; => #\e (string (elt "test" 1)) ;; => "e" ``` -------------------------------- ### Ensure String is Wrapped with Prefix and Suffix Source: https://github.com/vindarel/cl-str/blob/master/README.md Ensures a string both starts and ends with a specified prefix and suffix. It achieves this by calling `str:ensure-suffix` followed by `str:ensure-prefix`. If the string is already correctly wrapped, no changes are made. ```lisp (str:ensure-wrapped-in "/" "abc") ;; => "/abc/" (str:ensure-wrapped-in "/" "/abc/") ;; => "/abc/" ``` -------------------------------- ### Get Rest of String Source: https://github.com/vindarel/cl-str/blob/master/README.md Returns the substring of a given string excluding the first character. If the string is empty, it returns an empty string. ```lisp (s-rest "foobar") ;; => "oobar" (s-rest "") ;; => "" ``` -------------------------------- ### Get String Parts (First, Last, Rest, Nth) Source: https://github.com/vindarel/cl-str/blob/master/README.md These functions provide convenient ways to access parts of a string: `s-first` gets the first character, `s-last` gets the last character, `s-rest` gets the string without the first character, and `s-nth` gets the character at a specific index. ```common-lisp (str:s-first "abc") (str:s-last "abc") (str:s-rest "abc") (str:s-nth 1 "abc") ``` -------------------------------- ### Get Library Version (Common Lisp) Source: https://context7.com/vindarel/cl-str/llms.txt The `str:version` function prints and returns the current version of the cl-str library. The `str:+version+` constant also holds the version string. ```lisp (str:version) ;; Prints and returns the version string, e.g., "0.21" str:+version+ ;; => "0.21" ``` -------------------------------- ### Get Last Character of String Source: https://github.com/vindarel/cl-str/blob/master/README.md Returns the last character of a given string. Behavior for empty strings is not explicitly defined in the example but typically would return an empty string or error. ```lisp (s-last "foobar") ;; => "r" ``` -------------------------------- ### Get Nth Character of String Source: https://github.com/vindarel/cl-str/blob/master/README.md Returns the character at the specified index (0-based) within a string. If the index is out of bounds or the string is empty, it returns an empty string. ```lisp (s-nth 3 "foobar") ;; => "b" (s-nth 3 "") ;; => "" ``` -------------------------------- ### Check if String is Wrapped in Delimiter (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt The `wrapped-in-p` function verifies if a string starts and ends with a specified delimiter, returning the string if it is wrapped, and NIL otherwise. ```lisp (str:wrapped-in-p "/" "/path/") ;; => "/path/" (str:wrapped-in-p "/" "/path") ;; => NIL (str:wrapped-in-p "\"" "\"hello\"") ;; => "\"hello\"" ``` -------------------------------- ### Access String Characters and Substrings (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt Functions to get the first, last, nth character, or the rest of a string. These functions are safe for empty strings and nil inputs. ```lisp ;; Get first character as string (str:s-first "hello") ;; => "h" ;; Get last character as string (str:s-last "hello") ;; => "o" ;; Get rest of string (all but first) (str:s-rest "hello") ;; => "ello" ;; Get nth character as string (str:s-nth 1 "hello") ;; => "e" ;; Safe for empty strings and nil (str:s-first "") ;; => "" (str:s-first nil) ;; => NIL ``` -------------------------------- ### Split String from the End (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:rsplit` function splits a string into substrings starting from the end of the string. It is similar to `str:split` but the splitting direction is reversed. This difference is most apparent when a `:limit` is specified. ```lisp (separator s &key limit regex) ``` ```lisp (rsplit "/" "/var/log/mail.log" :limit 2) ;; => ("/var/log" "mail.log") ``` -------------------------------- ### Extract Substring with Flexible Bounds in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Extracts a portion of a string based on start and end indices. Handles nil/t for end bounds and negative indices gracefully, and manages out-of-bounds requests without errors. Dependencies: None. ```common-lisp ;; Basic extraction (str:substring 0 5 "hello world") ;; => "hello" ;; End can be nil or t for end of string (str:substring 6 nil "hello world") ;; => "world" (str:substring 6 t "hello world") ;; => "world" ;; Negative end counts from the end (str:substring 0 -1 "hello") ;; => "hell" ;; Out of bounds are handled gracefully (str:substring 0 100 "hello") ;; => "hello" (str:substring -10 5 "hello") ;; => "hello" ``` -------------------------------- ### Get Value from Association List by String Key (Common Lisp) Source: https://context7.com/vindarel/cl-str/llms.txt The `str:s-assoc-value` function retrieves a value from an association list using a string key. It supports case-insensitive matching for keys by default, making it flexible for configuration or data retrieval. It returns the cons cell if the value is found. ```lisp (let ((config '(("host" . "localhost") ("port" . 8080) ("debug" . t)))) (str:s-assoc-value config "host")) ;; => "localhost" ;; => ("host" . "localhost") ; second value is the cons cell ;; Case-insensitive matching (using string-equal) (str:s-assoc-value '(("Content-Type" . "text/html")) "content-type") ;; => "text/html" ``` -------------------------------- ### Create and Display ASCII Table using cl-ascii-table (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md This snippet illustrates the creation and display of an ASCII table using the `cl-ascii-table` library. It shows how to initialize a table with headers, add rows of data, add separators, and finally display the formatted table. ```lisp CL-USER> (ql:quickload "cl-ascii-table") CL-USER> (let ((table (ascii-table:make-table '("Id" "Name" "Amount") :header "Infos"))) (ascii-table:add-row table '(1 "Bob" 150)) (ascii-table:add-row table '(2 "Joe" 200)) (ascii-table:add-separator table) (ascii-table:add-row table '("" "Total" 350)) (ascii-table:display table)) .--------------------- | Infos | +----+-------+--------+ | Id | Name | Amount | +----+-------+--------+ | 1 | Bob | 150 | +----+-------+--------+ | 2 | Joe | 200 | +----+-------+--------+ | | Total | 350 | +----+-------+--------+ NIL ``` -------------------------------- ### Running Main Test Suite (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Provides instructions on how to execute the main test suite for the cl-str Common Lisp system using ASDF or by loading the test package and running fiveam tests. ```lisp (asdf:test-system :str) ``` ```lisp (load "str.test") (fiveam:run! 'test-str:str) ``` -------------------------------- ### Enabling Run-Test-When-Defined (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Explains how to configure the fiveam testing framework to automatically run tests after each definition or compilation, useful for TDD workflows. ```lisp (setf fiveam:*run-test-when-defined* t) ``` -------------------------------- ### Running Specific Test Suite (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Shows how to run a specific test suite, such as 'replace-functions', for the cl-str library using the fiveam testing framework. ```lisp (fiveam:run! 'test-str:replace-functions) ``` -------------------------------- ### Format String Padding (Alternative) Source: https://github.com/vindarel/cl-str/blob/master/README.md Demonstrates using the Common Lisp `format` function for string padding, offering an alternative to dedicated padding functions. ```lisp (format nil "~va" 10 "foo") ;; => "foo " (format nil "~v@a" 10 "foo") ;; => " foo" ``` -------------------------------- ### Extract Substring Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:substring` function extracts a portion of a string given a start and end index. The indices are 0-based and the end index is exclusive. ```common-lisp (str:substring 2 5 "abcdef") ``` -------------------------------- ### Running Unexported Test Function (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Demonstrates how to run a specific test for an unexported function, like `downcase`, within the cl-str library's test suite using fiveam. ```lisp (fiveam:run! 'test-str::downcase) ``` -------------------------------- ### Add Prefix or Suffix to Multiple Items Source: https://github.com/vindarel/cl-str/blob/master/README.md Prepends or appends a given string `s` to the beginning or end of each item in a collection. ```lisp (str:add-prefix "pre-" "item") (str:add-suffix "-suf" "item") ``` -------------------------------- ### Pattern Matching with Substring Binding and Regex (Common Lisp) Source: https://context7.com/vindarel/cl-str/llms.txt The experimental `str:match` macro enables pattern matching on strings, allowing for substring binding and the use of regular expressions. It supports placeholders like `_` for ignored parts and can extract and parse values from structured strings. It handles nil inputs gracefully. ```lisp ;; Extract values from structured strings (str:match "user:john:admin" (("user:" username ":" role) (list username role)) (t nil)) ;; => ("john" "admin") ;; Use _ as placeholder for ignored parts (str:match "john@gmail.com" ((_ "gmail" _) 'gmail-user) ((_ "yahoo" _) 'yahoo-user) (t 'other)) ;; => GMAIL-USER ;; With regex patterns (str:match "Order #12345 confirmed" (("Order #" order-num " confirmed") (parse-integer order-num)) (t nil)) ;; => 12345 ;; Regex in patterns (str:match "Price: $99.99" (("Price: $" "\\d+\\.\\d+" amount) amount) (t "unknown")) ``` -------------------------------- ### Count Substring Occurrences (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Counts the number of non-overlapping occurrences of a substring within a given string. The search can be limited to a specific range within the string using the `:start` and `:end` keyword arguments. ```lisp (count-substring "abc" "abcxabcxabc") ;; => 3 (count-substring "abc" "abcxabcxabc" :start 3 :end 7) ;; => 1 ``` -------------------------------- ### Fit String to Length Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:fit` function ensures a string is exactly a specified length. If the string is shorter, it's padded; if longer, it's truncated. This is useful for fixed-width displays. ```common-lisp (str:fit 10 "abc") (str:fit 3 "abcdef") ``` -------------------------------- ### Count Substring Occurrences (Common Lisp) Source: https://context7.com/vindarel/cl-str/llms.txt The `str:count-substring` function counts non-overlapping occurrences of a substring within a given string. It supports specifying start and end ranges for the search and can perform case-insensitive counting. ```lisp (str:count-substring "ab" "abracadabra") ;; => 2 (str:count-substring "the" "the cat in the hat") ;; => 2 ;; With range (str:count-substring "a" "abracadabra" :start 3 :end 8) ;; => 2 ;; Case-insensitive (str:count-substring "A" "abAba" :ignore-case t) ;; => 3 ``` -------------------------------- ### Check String Prefix and Suffix (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt Functions `starts-with-p` and `ends-with-p` check if a string begins or ends with a specified prefix or suffix, respectively. Case-insensitive comparisons are supported. ```lisp ;; Case-sensitive by default (str:starts-with-p "foo" "foobar") ;; => T (str:starts-with-p "Foo" "foobar") ;; => NIL ;; Ignore case (str:starts-with-p "FOO" "foobar" :ignore-case t) ;; => T ;; Check suffix (str:ends-with-p ".lisp" "hello.lisp") ;; => T ;; Works with characters too (str:ends-with-p #\/ "path/to/dir/") ;; => T ``` -------------------------------- ### Ensure String Prefix, Suffix, or Wrapping in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Ensures a string has a specific prefix, suffix, or is wrapped by characters. If the condition is not met, the required characters are added. Dependencies: None. ```common-lisp ;; Ensure prefix (adds if missing) (str:ensure-prefix "/" "path/to/file") ;; => "/path/to/file" (str:ensure-prefix "/" "/already/has/slash") ;; => "/already/has/slash" ;; Ensure suffix (str:ensure-suffix "/" "path/to/dir") ;; => "path/to/dir/" ;; Ensure wrapped (both prefix and suffix) (str:ensure-wrapped-in "\"" "hello") ;; => "\"hello\"" ;; Combined ensure function (str:ensure "abc" :prefix "/" :suffix "/") ;; => "/abc/" (str:ensure "foo" :wrapped-in "'") ;; => "'foo'" ``` -------------------------------- ### Ensure String Prefix/Suffix/Wrapping Source: https://github.com/vindarel/cl-str/blob/master/README.md These functions ensure a string has a specific prefix, suffix, or is wrapped by a given pair of strings. They are useful for formatting or validating string boundaries. `ensure` is a general function, while `ensure-prefix`, `ensure-suffix`, and `ensure-wrapped-in` provide more specific control. ```common-lisp (str:ensure s :wrapped-in "()") (str:ensure-prefix "pre-" s) (str:ensure-suffix "-suf" s) (str:ensure-wrapped-in "<>" s) ``` -------------------------------- ### Convert Between Naming Conventions Source: https://context7.com/vindarel/cl-str/llms.txt Provides functions to transform strings between various common naming conventions such as camelCase, PascalCase, snake_case, kebab-case, and more. ```lisp ;; camelCase (str:camel-case "hello world") ;; => "helloWorld" ;; PascalCase (str:pascal-case "hello world") ;; => "HelloWorld" ;; snake_case (str:snake-case "Hello World") ;; => "hello_world" ;; param-case (kebab-case) (str:param-case "Hello World") ;; => "hello-world" ;; CONSTANT_CASE (str:constant-case "hello world") ;; => "HELLO_WORLD" ;; Header-Case (str:header-case "hello world") ;; => "Hello-World" ;; dot.case (str:dot-case "Hello World") ;; => "hello.world" ;; path/case (str:path-case "Hello World") ;; => "hello/world" ;; Sentence case (str:sentence-case "HELLO WORLD") ;; => "Hello world" ;; Title Case (str:title-case "hello world") ;; => "Hello World" ;; swapCase (str:swap-case "Hello World") ;; => "hELLO wORLD" ``` -------------------------------- ### Fit String to Exact Length (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt The `fit` function adjusts a string to an exact length by padding or shortening. It allows customization of padding characters, side, and ellipsis. ```lisp ;; String shorter than length - pad (str:fit 10 "hello") ;; => "hello " ;; String longer than length - shorten (str:fit 8 "hello world") ;; => "hello..." ;; Exact fit - no change (str:fit 5 "hello") ;; => "hello" ;; With custom options (str:fit 10 "hi" :pad-char #\- :pad-side :center) ;; => "----hi----" (str:fit 8 "hello world" :ellipsis "~") ;; => "hello w~" ``` -------------------------------- ### Pad String to a Specific Length Source: https://github.com/vindarel/cl-str/blob/master/README.md Fills a string with padding characters until it reaches a specified length. Padding can be applied to the right (default), left, or center. The padding character can also be specified. ```lisp (str:pad 10 "foo") ;; => "foo " (str:pad 10 "foo" :pad-side :center :pad-char "+") ;; => "+++foo++++" ``` -------------------------------- ### Convert String Case Source: https://context7.com/vindarel/cl-str/llms.txt Converts strings to lowercase, uppercase, or capitalized format. These functions are nil-safe, returning nil for nil input, unlike some built-in functions. ```lisp ;; Returns nil for nil input (not "nil" string!) (str:downcase nil) ;; => NIL (string-downcase nil) ;; => "nil" (built-in surprise) (str:downcase "HELLO") ;; => "hello" (str:upcase "hello") ;; => "HELLO" (str:capitalize "hello world") ;; => "Hello World" ;; Works with symbols and keywords (str:downcase :HELLO) ;; => "hello" ``` -------------------------------- ### Check for Empty or Blank Strings (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt The `emptyp` function checks if a string is nil or empty, while `blankp` checks if it's empty or contains only whitespace. Type-checking variants like `non-empty-string-p` are also available. ```lisp ;; emptyp - nil or empty string (str:emptyp nil) ;; => T (str:emptyp "") ;; => T (str:emptyp " ") ;; => NIL ;; blankp - empty or only whitespace (str:blankp "") ;; => T (str:blankp " ") ;; => T (str:blankp " a ") ;; => NIL ;; Type-checking variants (str:non-empty-string-p "hello") ;; => T (str:non-empty-string-p nil) ;; => NIL (str:non-blank-string-p " ") ;; => NIL ``` -------------------------------- ### Pad String to Specific Length in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Fills a string with a specified padding character (defaulting to space) on the left, right, or center to reach a target length. Dependencies: None. ```common-lisp ;; Default padding (right side, spaces) (str:pad 10 "foo") ;; => "foo " ;; Pad left (str:pad-left 10 "foo") ;; => " foo" ;; Pad center (str:pad-center 10 "foo") ;; => " foo " ;; Custom padding character (str:pad 10 "foo" :pad-char #\-) ;; => "foo------- M M" ;; Combined options (str:pad 10 "foo" :pad-side :center :pad-char #\+) ;; => "+++foo++++" ``` -------------------------------- ### Shorten String with Global Ellipsis Source: https://github.com/vindarel/cl-str/blob/master/README.md Demonstrates shortening a string using a globally defined ellipsis character or string via `*ellipsis*`. ```lisp (let ((*ellipsis* "-")) (shorten 8 "hello world")) ;; => "hello w-" ``` -------------------------------- ### Safe Case Conversion (Downcase, Upcase, Capitalize) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:downcase`, `str:upcase`, and `str:capitalize` functions provide safe versions of their built-in counterparts. They return a new string and crucially return nil when the input is nil, unlike the built-ins which return string representations of nil. ```common-lisp (str:downcase nil) ;; nil (str:upcase nil) ;; nil (str:capitalize nil) ;; nil (str:downcase :FOO) ;; "foo" ``` -------------------------------- ### Read from and Write to Files Source: https://context7.com/vindarel/cl-str/llms.txt Provides functions to read the entire content of a file into a string and write a string to a file. Supports specifying file encoding and append mode. ```lisp ;; Read entire file as string (str:from-file "config.txt") ;; => "file contents..." ;; Read with specific encoding (str:from-file "data.txt" :external-format :utf-8) ;; Write string to file (str:to-file "output.txt" "Hello, World!") ;; => "Hello, World!" ;; Append to file (str:to-file "log.txt" "New entry" :if-exists :append) ``` -------------------------------- ### Find Common Prefix or Suffix of Strings Source: https://context7.com/vindarel/cl-str/llms.txt Determines the longest common prefix or suffix among a list of strings. Returns an empty string if no common prefix or suffix is found. ```lisp (str:prefix '("foobar" "foobaz" "fooqux")) ;; => "foo" (str:suffix '("test.lisp" "main.lisp" "utils.lisp")) ;; => ".lisp" (str:prefix '("abc" "xyz")) ;; => "" ``` -------------------------------- ### Join List of Strings Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:join` function concatenates a list of strings into a single string, using a specified separator between each element. This is a more readable alternative to using `format` for string concatenation. ```common-lisp (str:join separator list-of-strings) ``` -------------------------------- ### Experimental String Matching Macro (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md A COND-like macro for matching substrings within a string and binding parts of the match to variables. It supports regular expressions for matching and uses `_` as a wildcard. This macro is experimental and subject to change. ```lisp (str:match "a 1 b 2 d" (("a " x " b " y " d") ;; => matched (+ (parse-integer x) (parse-integer y))) (t 'default-but-not-for-this-case)) ;; default branch ;; => 3 (str:match "a 1 b c d" (("a 2 b" _ "d") ;; => not matched (print "pass")) (("a " _ " b c d") ;; => matched "here we go") (t 'default-but-not-for-this-case)) ;; default branch ;; => "here we go" (str:match "123 hello 456" (("\d+" s "\d+") s) (t "nothing")) ;; => " hello " ``` -------------------------------- ### Add Prefix or Suffix to List Items in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Applies a given prefix or suffix to each string within a list, returning a new list with the modified strings. Dependencies: None. ```common-lisp (str:add-prefix '("bar" "baz") "foo-") ;; => ("foo-bar" "foo-baz") (str:add-suffix '("test" "spec") ".lisp") ;; => ("test.lisp" "spec.lisp") ``` -------------------------------- ### Add Prefix/Suffix to String Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:add-prefix` and `str:add-suffix` functions prepend or append a given string or character to another string, respectively. These are convenient for simple prefixing and suffixing operations. ```common-lisp (str:add-prefix "pre-" s) (str:add-suffix "-suf" s) ``` -------------------------------- ### Repeat String Multiple Times in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Creates a new string by repeating a given string a specified number of times. Efficient for large counts. Dependencies: None. ```common-lisp (str:repeat 3 "foo") ;; => "foofoofoo" (str:repeat 5 "-") ;; => "-----" (str:repeat 10 "ab") ;; => "abababababababababab" ;; Works efficiently for large counts (length (str:repeat 100000 "x")) ;; => 100000 ``` -------------------------------- ### Split String into Words/Lines Source: https://github.com/vindarel/cl-str/blob/master/README.md These functions convert between strings and lists of strings. `words` splits a string by whitespace, `unwords` joins a list of strings with spaces. `lines` splits by newline characters, and `unlines` joins with newlines. ```common-lisp (str:words "Hello world example") (str:unwords '("Hello" "world" "example")) (str:lines "Line1\nLine2\nLine3") (str:unlines '("Line1" "Line2" "Line3")) ``` -------------------------------- ### String Case Macro (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md A case-like macro that performs comparisons based on string equality, unlike the standard `case` macro which uses `eql`. It allows matching against single strings or lists of strings. ```lisp (str:string-case "hello" ("foo" 1) (("hello" "test") 5) (nil (print "input is nil")) (otherwise (print "non of the previous forms was caught."))) (trivia:match "hey" ("hey" (print "it matched")) (otherwise :nothing)) ``` -------------------------------- ### Split and Join by Whitespace (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt The `words` function splits a string into a list of words using whitespace as a delimiter, while `unwords` joins a list of words into a single string separated by spaces. ```lisp ;; Split into words (str:words " hello world ") ;; => ("hello" "world") ;; Join words (str:unwords '("hello" "world")) ;; => "hello world" ``` -------------------------------- ### Replace Multiple Patterns in a String Source: https://context7.com/vindarel/cl-str/llms.txt Replaces multiple patterns within a string simultaneously using a list of old/new pairs. Supports both literal and regex-based replacements. ```lisp ;; List of old/new pairs (str:replace-using '("{{name}}" "John" "{{age}}" "30") "Hello {{name}}, you are {{age}} years old.") ;; => "Hello John, you are 30 years old." ;; With regex (str:replace-using '("fo+" "X" "ba+" "Y") "foo bar baz" :regex t) ;; => "X Yr Yz" ``` -------------------------------- ### Write String to File (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:to-file` function writes a given string to a specified file. If the file does not exist, it is created. If it exists, it can be replaced or appended to, depending on the `:if-exists` option. The function returns the string that was written. ```lisp (filename s) ``` -------------------------------- ### Case Conversion Functions Source: https://github.com/vindarel/cl-str/blob/master/README.md Provides various case conversion functions like camelCase, snake_case, PascalCase, etc., adapted from the cl-change-case library. These functions handle symbols and characters, and return nil for nil input. ```common-lisp :no-case (s &key replacement) :camel-case (s &key merge-numbers) :dot-case :header-case :param-case :pascal-case :path-case :sentence-case :snake-case :swap-case :title-case :constant-case ``` -------------------------------- ### Trim Whitespace and Custom Characters in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Removes leading and trailing whitespace or specified characters from a string. Supports left and right trimming variants and handles nil input gracefully. Dependencies: None directly, but relies on cl-ppcre for advanced regex if used. ```common-lisp ;; Basic whitespace trimming (str:trim " hello world ") ;; => "hello world" ;; Trim custom characters (str:trim "+-*foo-bar*-+" :char-bag "+ M") ;; => "foo-bar" ;; Trim with character list (str:trim "abchelloacb" :char-bag (list #\a #\b #\c)) ;; => "hello" ;; trim-left and trim-right variants (str:trim-left " hello ") ;; => "hello " (str:trim-right " hello ") ;; => " hello" ;; Returns nil for nil input (not "nil" string) (str:trim nil) ;; => NIL ``` -------------------------------- ### Check for Empty or Nil String Source: https://github.com/vindarel/cl-str/blob/master/README.md The `emptyp` function checks if a given string is nil or an empty string. It returns true for nil and "", but false for strings containing only whitespace. ```common-lisp (emptyp nil) ;; => T (emptyp "") ;; => T (emptyp " ") ;; => NIL ``` -------------------------------- ### Replace Occurrences in a String Source: https://context7.com/vindarel/cl-str/llms.txt Replaces all or the first occurrence of a substring within a given string. Supports literal replacement and regex-based replacement. ```lisp ;; Replace all occurrences (str:replace-all "foo" "bar" "foo foo foo") ;; => "bar bar bar" ;; Replace first occurrence only (str:replace-first "foo" "bar" "foo foo foo") ;; => "bar foo foo" ;; Special regex characters are treated literally by default (str:replace-all "+" "-" "a+b+c") ;; => "a-b-c" ;; Enable regex mode (str:replace-all "fo+" "bar" "foo fooo fo" :regex t) ;; => "bar bar bar" (str:replace-first "\d+" "X" "abc123def456" :regex t) ;; => "abcXdef456" ``` -------------------------------- ### Collapse Multiple Whitespaces to Single Space in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Reduces sequences of multiple whitespace characters (including newlines) to a single space between words. Ensures a clean, single-spaced output. Dependencies: None. ```common-lisp (str:collapse-whitespaces "foo bar\n\n baz") ;; => "foo bar baz" (str:collapse-whitespaces "hello world") ;; => "hello world" ``` -------------------------------- ### Split String by Separator (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt The `split` function divides a string into a list of substrings based on a specified separator. It supports character, string, and regex separators, and can omit empty strings or limit the number of splits. ```lisp ;; Basic split (str:split " " "foo bar baz") ;; => ("foo" "bar" "baz") ;; Split with character separator (str:split #\, "a,b,c") ;; => ("a" "b" "c") ;; Keeps empty strings by default (str:split "," "a,,b") ;; => ("a" "" "b") ;; Omit empty strings (str:split "," "a,,b" :omit-nulls t) ;; => ("a" "b") ;; Limit number of splits (str:split "/" "/var/log/mail.log" :limit 3) ;; => ("" "var" "log/mail.log") ;; Use regex separator (str:split "[,;]" "a,b;c" :regex t) ;; => ("a" "b" "c") ``` -------------------------------- ### Change String Case Source: https://github.com/vindarel/cl-str/blob/master/README.md The library provides functions for changing string case, including `downcase`, `upcase`, and `capitalize`, which fix surprising behaviors of built-in functions (e.g., `(str:downcase nil)` returns `nil`, not the string "nil"). It also includes predicates like `downcasep` and `upcasep`. ```common-lisp (str:downcase "HeLlO") (str:upcase "HeLlO") (str:capitalize "hello world") (str:downcase nil) (str:downcasep "hello") (str:upcasep "HELLO") ``` -------------------------------- ### Fit String to Length with Padding or Ellipsis (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:fit` function adjusts a string to a specified length. It can shorten a string by adding an ellipsis if it's too long, or pad it with a specified character if it's too short. This function accepts arguments similar to `str:shorten` and `str:pad`. ```lisp CL-USER> (str:fit 10 "hello" :pad-char "+") "hello+++++" CL-USER> (str:fit 10 "hello world" :ellipsis "…") "hello wor…" ``` -------------------------------- ### Concatenate Strings in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Joins multiple strings together into a single string. If no arguments are provided, it returns an empty string. Dependencies: None. ```common-lisp (str:concat "foo" "bar" "baz") ;; => "foobarbaz" (str:concat "Hello, " "World" "!") ;; => "Hello, World!" (str:concat) ;; => "" ``` -------------------------------- ### Check if String Starts/Ends With Substring Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:starts-with-p` and `str:ends-with-p` functions check if a string begins or ends with a specified substring, respectively. They support case-insensitive comparisons via the `:ignore-case` argument. ```common-lisp (str:starts-with-p "http://" url) (str:ends-with-p ".com" domain :ignore-case t) ``` -------------------------------- ### Split and Join by Newlines (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt The `lines` function splits a string into a list of lines based on newline characters, and `unlines` joins a list of strings into a single string with newlines separating each element. Both support omitting empty lines. ```lisp ;; Split into lines (str:lines "line1 line2 line3") ;; => ("line1" "line2" "line3") ;; Join lines (str:unlines '("line1" "line2" "line3")) ;; => "line1 ;; line2 ;; line3" ;; Omit empty lines (str:lines "a b" :omit-nulls t) ;; => ("a" "b") ``` -------------------------------- ### String Case Macro for Conditional String Execution (Common Lisp) Source: https://context7.com/vindarel/cl-str/llms.txt The `str:string-case` macro provides a way to conditionally execute code based on string input, similar to a switch statement. It supports single string matches, multiple string matches, and a fallback 'otherwise' case. It handles nil inputs gracefully. ```lisp (str:string-case user-input ("help" (show-help)) ("quit" (quit-application)) (("yes" "y") (confirm-action)) (otherwise (format t "Unknown command: ~a" user-input))) ;; Example with return value (str:string-case file-extension (".lisp" 'common-lisp) (".py" 'python) ((".js" ".ts") 'javascript) (otherwise 'unknown)) ``` -------------------------------- ### Join List of Strings with Separator in Common Lisp Source: https://context7.com/vindarel/cl-str/llms.txt Joins elements of a list of strings using a specified separator, which can be a string or a character. A nil separator joins elements without any delimiter. Dependencies: None. ```common-lisp ;; Join with string separator (str:join ", " '("apple" "banana" "cherry")) ;; => "apple, banana, cherry" ;; Join with character separator (str:join #\- '("2024" "01" "15")) ;; => "2024-01-15" ;; Join with multi-character separator (str:join " :: " '("foo" "bar" "baz")) ;; => "foo :: bar :: baz" ;; nil separator joins without delimiter (str:join nil '("a" "b" "c")) ;; => "abc" ``` -------------------------------- ### Read File Content to String (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:from-file` function reads the entire content of a specified file and returns it as a single string. It allows specifying the external format for reading the file, defaulting to the system's default if not provided. It also notes the availability of `uiop:read-file-string` and `uiop:read-file-lines`. ```lisp (filename) ``` ```lisp (str:from-file "path/to/file.txt") ``` -------------------------------- ### Read String from File / Write String to File Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:from-file` function reads the entire content of a file into a string. Conversely, `str:to-file` writes a given string to a specified file. These functions simplify file I/O operations for string data. ```common-lisp (str:from-file "path/to/your/file.txt") (str:to-file "path/to/output.txt" "This content will be written to the file.") ``` -------------------------------- ### Check for Blank String (Empty or Whitespace Only) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `blankp` function determines if a string is either empty or consists solely of whitespace characters. This is distinct from `emptyp`, which only checks for nil or an empty string. ```common-lisp (blankp "") ;; => T (blankp " ") ;; => T ``` -------------------------------- ### Check Common Prefix/Suffix Among Items (cl-str) Source: https://context7.com/vindarel/cl-str/llms.txt The `prefixp` and `suffixp` functions check if a given string is a common prefix or suffix among all items in a list. They return the prefix/suffix if common, otherwise NIL. ```lisp ;; Returns the prefix if all items share it (str:prefixp '("foobar" "foobaz") "foo") ;; => "foo" (str:prefixp '("foobar" "barfoo") "foo") ;; => NIL (str:suffixp '("test.lisp" "main.lisp") ".lisp") ;; => ".lisp" ``` -------------------------------- ### Remove Punctuation from String Source: https://context7.com/vindarel/cl-str/llms.txt Removes punctuation characters from a string. Allows for custom replacement characters. ```lisp (str:remove-punctuation "Hello, World! How are you?") ;; => "Hello World How are you" ;; Custom replacement (str:remove-punctuation "a,b,c" :replacement "-") ;; => "a-b-c" ``` -------------------------------- ### Split String into Words (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:words` function splits a string into a list of words, using whitespace as the delimiter. This is a common utility for tokenizing input strings. ```lisp (s) ``` -------------------------------- ### Ensure String Prefixes/Suffixes in cl-str Source: https://github.com/vindarel/cl-str/blob/master/README.md The `ensure` function adds a prefix or suffix to a string if it's missing, or wraps the string. It checks for `:wrapped-in`, `:prefix`, and `:suffix` parameters in order. The function accepts strings and characters. ```Common Lisp (str:ensure "abc" :wrapped-in "/") ;; => "/abc/" (str:ensure "/abc" :prefix "/") ;; => "/abc" => no change, still one "/" (str:ensure "/abc" :suffix "/") ;; => "/abc/" => added a "/" suffix. ``` ```Common Lisp (str:ensure "/abc" :prefix #\/) ``` -------------------------------- ### Character Type Predicates for Strings Source: https://context7.com/vindarel/cl-str/llms.txt Provides functions to check the character composition of strings, including ASCII letters, Unicode letters, digits, and alphanumeric characters. Also checks for the presence of specific character types within a string. ```lisp ;; Only ASCII letters [a-zA-Z] (str:alphap "Hello") ;; => T (str:alphap "Hello123") ;; => NIL (str:alphap "Helloé") ;; => NIL ;; Letters including unicode (str:lettersp "Helloéß") ;; => T ;; Alphanumeric [a-zA-Z0-9] (str:alphanump "Hello123") ;; => T (str:alphanump "Hello 123") ;; => NIL (space) ;; Unicode letters and numbers (str:lettersnump "éß123") ;; => T ;; Only digits (str:digitp "12345") ;; => T (str:digitp "123.45") ;; => NIL ;; Check for presence of character types (str:has-alpha-p "123abc") ;; => T (str:has-letters-p "123é") ;; => T (str:has-alphanum-p "!!1!!") ;; => T ``` -------------------------------- ### String-Aware Association List Value Retrieval (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Retrieves the value associated with a string key from an association list. It returns the value and the matching cons cell. The argument order is reversed compared to `cl:assoc` for consistency with other libraries. ```lisp (s-assoc-value '(("hello" . 1)) "hello") ;; 1 ;; ("hello" . 1) (alexandria:assoc-value '(("hello" . 1)) "hello") ;; NIL (alexandria:assoc-value '(("hello" . 1)) "hello" :test #'string=) ;; 1 ;; ("hello" . 1) (assoc "hello" '(("hello" . 1))) ;; NIL (assoc "hello" '(("hello" . 1)) :test #'string=) ;; ("hello" . 1) (cdr *) ;; 1 ``` -------------------------------- ### Check if String is Empty or Blank Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:emptyp` function returns true if the string is empty (length 0). `str:blankp` returns true if the string is empty or contains only whitespace characters. ```common-lisp (str:emptyp "") (str:blankp " ") ``` -------------------------------- ### Join List of Strings with Blank Lines (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:unparagraphs` function joins a list of strings with a blank line (two newline characters) between each element. This is the inverse operation of `str:paragraphs` in terms of joining, though whitespace trimming in `str:paragraphs` means the round trip may not be exact. ```lisp unparagraphs ``` -------------------------------- ### Split String Behavior Change (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Demonstrates a backward-incompatible change in the `str:split` function's behavior regarding trailing separators. Previously, a trailing separator did not result in an empty string at the end of the output list, similar to `cl-ppcre:split`. Now, it correctly includes a trailing empty string. ```lisp (str:split " " "a b c ") ;; Before: ("a" "b" "c") ;; Now: ("a" "b" "c" "") ``` -------------------------------- ### Replace All Occurrences in String (Common Lisp) Source: https://github.com/vindarel/cl-str/blob/master/README.md Replaces all occurrences of a substring with another string. Similar to replace-first, it supports treating the substring as a regular expression. For single-character replacements, the built-in `substitute` function can be more efficient. ```lisp (replace-all "a" "o" "faa") ;; => "foo" (replace-all "fo+" "frob" "foofoo bar" :regex t) ;; => "frobfrob bar" (substitute #\+ #\Space "foo bar baz") ;; "foo+bar+baz" ``` -------------------------------- ### Concatenate Strings Source: https://github.com/vindarel/cl-str/blob/master/README.md The `str:concat` function concatenates multiple strings into a single string. It accepts a variable number of string arguments. ```common-lisp (str:concat "Hello" " " "World!") ```