Strings
Learn how to work with strings in Kronos
Strings
Strings are sequences of characters and support various operations.
Creating Strings
set greeting to "Hello, World!"
set empty to ""
set multiline to "Line 1\nLine 2"String Concatenation
Use the plus operator to concatenate strings:
set first to "Hello"
set second to "World"
set message to first plus ", " plus second plus "!"
print message # Prints: Hello, World!String Indexing
Access individual characters using indexing:
set text to "Kronos"
set first_char to text at 0 # "K"
set last_char to text at -1 # "s"String Slicing
Extract substrings using the from keyword:
set text to "Programming"
set slice1 to text from 0 to 4 # "Prog"
set slice2 to text from 3 to 7 # "gram"
set slice3 to text from 5 to end # "mming"F-Strings (String Interpolation)
F-strings allow you to embed expressions inside strings:
set name to "Alice"
set greeting to f"Hello, {name}!"
print greeting # Prints: Hello, Alice!
set x to 10
set y to 20
set result to f"{x} plus {y} equals {x plus y}"
print result # Prints: 10 plus 20 equals 30You can use any expression inside {}:
set text to "Hello"
set info to f"'{text}' has {call len with text} characters"Format Specifiers
F-strings support format specifiers for precise control over value formatting. Add a colon followed by a format specification after the expression: {expression:format_spec}.
Precision (Decimal Places)
set price to 19.99
print f"Price: {price:.2f}" # Price: 19.99
set pi to 3.14159265
print f"Pi: {pi:.4f}" # Pi: 3.1416
print f"Pi: {pi:.0f}" # Pi: 3Width and Alignment
Control the minimum width and alignment of the output:
set x to 42
print f"|{x:10}|" # | 42| (right-aligned by default)
print f"|{x:<10}|" # |42 | (left-aligned)
print f"|{x:>10}|" # | 42| (right-aligned)
print f"|{x:^10}|" # | 42 | (centered)Fill Characters
Specify a fill character before the alignment:
set num to 7
print f"{num:0>5}" # 00007 (zero-padded)
print f"{num:->5}" # ----7 (dash-padded)
print f"{num:*^9}" # ****7**** (centered with stars)Type Specifiers
Explicitly specify the output type:
set value to 123.456
print f"As integer: {value:d}" # As integer: 123
print f"As float: {value:.1f}" # As float: 123.5
print f"As string: {value:s}" # As string: 123.456Complete Format Syntax
The full format specifier syntax is:
{expression:[[fill]align][width][.precision][type]}| Component | Options | Description |
|---|---|---|
fill | Any character | Character used for padding (default: space) |
align | < > ^ | Left, right, or center alignment |
width | Integer | Minimum field width |
.precision | . + integer | Decimal places for floats, max length for strings |
type | d f s | Integer, float, or string format |
Practical Examples
# Currency formatting
set price to 9.5
print f"Total: ${price:>7.2f}" # Total: $ 9.50
# Table formatting
set name to "Item"
set qty to 5
set unit_price to 19.99
print f"{name:<10} x {qty:>3d} @ ${unit_price:>7.2f}"
# Output: Item x 5 @ $ 19.99
# ID formatting
set id to 42
print f"ID: {id:0>6}" # ID: 000042String Functions
Kronos provides many built-in string functions. See the String Functions API for the complete reference.
set text to " Hello World "
call len with text # Length
call uppercase with text # " HELLO WORLD "
call trim with text # "Hello World"
call split with "a,b,c", "," # ["a", "b", "c"]
call contains with text, "Hello" # true
call replace with text, "World", "Kronos"Examples
Parsing Strings
set data to "name:Alice,age:30,city:NYC"
set parts to call split with data, ","
for part in parts:
set key_value to call split with part, ":"
set key to key_value at 0
set value to key_value at 1
print f"{key}: {value}"Building Strings
set items to list "apple", "banana", "cherry"
set result to call join with items, ", "
print result # Prints: apple, banana, cherry