Strings

functions and helpers for manipulating and dealing with strings or character blocks


Functions

Predicates


The Strings module provides comprehensive functionality for string manipulation, from basic operations to advanced templating. It handles strings as Unicode character collections and offers powerful pattern matching capabilities.

Key Concepts

  • Full Unicode support
  • String interpolation and templating
  • Multiple string creation syntaxes
  • Pattern matching and substitution
  • Case conversion and manipulation

Basic Usage

String Creation

; basic strings
str: "hello world"

; multi-line strings
text: {
    This is a multi-line string
    that preserves formatting
    but strips common indentation
}

; verbatim strings (keep exact formatting)
code: {:
    def main():
        print("hello")
:}

; right smart-quote strings (everything until end of line)
message: « This is a single line string

; heredoc-style
data: 
------
Everything after the dashes
until the end of the file
is part of the string
------

Basic Operations

; concatenation
full: "hello " ++ "world"    ; hello world

; case conversion
upper "hello"               ; HELLO
lower "WORLD"               ; world
capitalize "john"           ; John

; checking content
prefix? "hello" "he"        ; => true
suffix? "hello" "lo"        ; => true
contains? "hello" "ll"      ; => true

; whitespace handling
strip " hello  "            ; hello
strip.start "  hi"          ; hi
strip.end "hi  "            ; hi

Pattern Matching

text: "hello world"

; using regex
match text {/o(.+)d/}    ; "o world"

; capturing matches
match.capture text {/([wrl]+)o/} 
; => ["ll" "w"]

; replace patterns
final: replace "hi there" "hi" "hello"
print final                  ; hello there

; with regex
result: replace "Date: 2024" {/\d+/} "2025"
print result                ; Date: 2025

Common Patterns

String Manipulation

; word wrapping
text: "This is a very long line that needs to be wrapped"
print wordwrap.at:20 text
; This is a very long
; line that needs to
; be wrapped

; truncating
print truncate "Hello World" 5      ; Hello...
print truncate.with:"..." "Hello World" 5  ; Hello...

; padding
print pad.right "Price:" 10         ; Price:    
print pad.center "Title" 20         ; -------Title--------

Working with Parts

text: "hello world"

; splitting
words: split.words text            
; ["hello" "world"]
chars: split text                  
; ["h" "e" "l" "l" "o" " " "w" "o" "r" "l" "d"]

; joining
print join.with:", " ["a" "b" "c"]      ; a, b, c

; slicing
print slice text 0 4                    ; hello

String Templates

Arturo's templating system uses pipe characters (|...|) for interpolation:

Simple Interpolation

name: "Bill"
age: 38

; basic interpolation
print ~"Hello |name|!"                  ; Hello Bill!
print ~"I am |age| years old"           ; I am 38 years old

; with expressions
print ~"Next year I'll be |age + 1|"    ; Next year I'll be 39

Conditional Templates

age: 30
template: {
    <||= (age > 21)? [ ||>
        You can enter the club
    <||][||>
        Sorry, too young
    <||]||>
}

print render.template template    
; You can enter the club

Loops in Templates

items: ["apple" "banana" "orange"]
template: {
    <ul>
        <|| loop items 'item [||>
            <li>
                <||= item ||>
            </li>
        <||]||>
    </ul>
}

print render.template template
; <ul>
;     <li>
;         apple
;     </li>
;     <li>
;         banana
;     </li>
;     <li>
;         orange
;     </li>
; </ul>

Advanced Template Features

users: @[
    #[name: "John" role: "admin"]
    #[name: "Alice" role: "user"]
]

template: {
    Users:
    <|| loop users 'user [||>
        * <||= user\name ||> 
          <||= user\role = "admin" [||>
            (Administrator)
          <||]||>
    <||]||>
}

print render.template template
; Users:
;     * John 
;       true
;     * Alice 
;       false