str
Strings are immutable sequences of UTF-8 bytes.
Fields
len
Returns the byte length of the string.
Type: int
Methods
starts_with prefix
Tests whether the string starts with the given prefix.
Parameters:
| Name | Type | Description |
|---|---|---|
prefix |
str |
the prefix string |
Returns: bool
without_prefix prefix
Returns the string with the prefix removed if it matches, otherwise returns the original string.
Parameters:
| Name | Type | Description |
|---|---|---|
prefix |
str |
the prefix to remove |
Returns: str
ends_with suffix
Tests whether the string ends with the given suffix.
Parameters:
| Name | Type | Description |
|---|---|---|
suffix |
str |
the suffix string |
Returns: bool
without_suffix suffix
Returns the string with the suffix removed if it matches, otherwise returns the original string.
Parameters:
| Name | Type | Description |
|---|---|---|
suffix |
str |
the suffix to remove |
Returns: str
split delimiter [limit: int]
Splits the string by the delimiter, returning an iterator that yields segments in left-to-right order.
The optional limit controls how many splits are performed and from which end:
limit: N(positive) — split at most N times from the left; the last element is the unsplit remainder.limit: -N(negative) — split at most N times from the right, but still yield segments left-to-right. Useful for splitting off a known-length suffix (e.g. a file extension).- Omitted — split fully with no limit.
Parameters:
| Name | Type | Description |
|---|---|---|
delimiter |
str |
the delimiter string |
limit |
int |
max splits; negative means split from right |
Returns: iterator of str
assert_eq [..."a,b,c".split ","] ["a", "b", "c"]
assert_eq [..."a,b,c".split "," limit: 1] ["a", "b,c"]
# Negative limit: split from the right, yield left-to-right
let base ext = "archive.tar.gz".split "." limit: -1
assert_eq $base "archive.tar"
assert_eq $ext "gz"
rsplit delimiter [limit: int]
Like split, but yields segments in right-to-left order (rightmost segment
first).
The optional limit controls how many splits are performed and from which end:
limit: N(positive) — split at most N times from the right; the last element yielded is the unsplit left remainder.limit: -N(negative) — split at most N times from the left, but still yield segments right-to-left.- Omitted — split fully with no limit.
Parameters:
| Name | Type | Description |
|---|---|---|
delimiter |
str |
the delimiter string |
limit |
int |
max splits; negative means split from left |
Returns: iterator of str
assert_eq [..."a,b,c".rsplit ","] ["c", "b", "a"]
assert_eq [..."a,b,c".rsplit "," limit: 1] ["c", "a,b"]
# Negative limit: split from the left, yield right-to-left
assert_eq [..."a,b,c".rsplit "," limit: -1] ["b,c", "a"]
join iter?
Joins values from an input source using this string as a separator.
Parameters:
| Name | Type | Description |
|---|---|---|
input |
iterable to join (uses default input if omitted) |
Returns: str
trim chars?
Removes whitespace (or specified characters) from both ends.
Parameters:
| Name | Type | Description |
|---|---|---|
chars |
str |
characters to trim (defaults to whitespace) |
Returns: str
trim_start chars?
Removes whitespace (or specified characters) from the start.
Parameters:
| Name | Type | Description |
|---|---|---|
chars |
str |
characters to trim |
Returns: str
trim_end chars?
Removes whitespace (or specified characters) from the end.
Parameters:
| Name | Type | Description |
|---|---|---|
chars |
str |
characters to trim |
Returns: str
contains needle
Tests whether the string contains the given substring.
Parameters:
| Name | Type | Description |
|---|---|---|
needle |
str |
the substring to find |
Returns: bool
assert ("foobar".contains "foo")
assert ("foobar".contains "bar")
assert (!"foobar".contains "baz")
assert ("foobar".contains "") # empty string is always contained
replace from to
Returns a new string with all non-overlapping occurrences of from replaced
with to.
Parameters:
| Name | Type | Description |
|---|---|---|
from |
str |
substring to replace |
to |
str |
replacement string |
Returns: str
assert_eq ("foo bar foo".replace "foo" "baz") "baz bar baz"
assert_eq ("banana".replace "na" "") "ba"
assert_eq ("abc".replace "" "-") "-a-b-c-"
sub start end?
Returns a substring from start to end (exclusive). If end is omitted,
returns from start to the end of the string. Negative indexes count from the
end using byte offsets.
Parameters:
| Name | Type | Description |
|---|---|---|
start |
int |
starting byte index |
end |
int |
ending byte index (exclusive) |
Returns: str
assert_eq ("foobar".sub 3) "bar"
assert_eq ("foobar".sub 2 4) "ob"
assert_eq ("foobar".sub -3) "bar"
assert_eq ("foobar".sub 1 -1) "ooba"
upper
Returns the string converted to uppercase.
Returns: str
lower
Returns the string converted to lowercase.
Returns: str
repeat count
Returns the string repeated count times.
Parameters:
| Name | Type | Description |
|---|---|---|
count |
int |
non-negative repetition count |
Returns: str