Module

pooka/string

src/pooka/string.pooka

Text has letters, bytes and occasionally a combining mark travelling alone.

Represent UTF-8 strings and expose code-point operations. Length, slicing and to_list count Unicode code points, not bytes or grapheme clusters. Slice offsets are clamped through list operations; use byte-oriented APIs when encoding layout matters.

Pooka
import pooka/string

assert string.slice("🐇oak", 1, 2) == "oa"

API reference

function

length

src/pooka/string.pooka:53
length(text: String) -> Int
INFERREDfn(String) -> Int

Count Unicode code points, including combining marks as their own points.

Pooka
import pooka/string

assert string.length("🐇oak") == 4
function

is_empty

src/pooka/string.pooka:66
is_empty(text: String) -> Bool
INFERREDfn(String) -> Bool

Ask whether the string contains no bytes and no code points.

Pooka
import pooka/string

assert string.is_empty("")
function

concat

src/pooka/string.pooka:77
concat(left: String, right: String) -> String
INFERREDfn(String, String) -> String

Append the second UTF-8 string to the first.

Pooka
import pooka/string

assert string.concat("oak", "leaf") == "oakleaf"
function

join

src/pooka/string.pooka:88
join(parts: List(String), separator: String) -> String
INFERREDfn(List(String), String) -> String

Place the separator between strings, without adding one at either end.

Pooka
import pooka/string

assert string.join(["oak", "pine"], ", ") == "oak, pine"
function

split

src/pooka/string.pooka:100
split(text: String, separator: String) -> List(String)
INFERREDfn(String, String) -> List(String)

Split on a separator; an empty separator yields individual Unicode code points.

Pooka
import pooka/string

assert string.split("oak:pine", ":") == ["oak", "pine"]
function

contains

src/pooka/string.pooka:111
contains(text: String, needle: String) -> Bool
INFERREDfn(String, String) -> Bool

Test for a substring. Every string contains the empty string.

Pooka
import pooka/string

assert string.contains("oakleaf", "leaf")
function

starts_with

src/pooka/string.pooka:122
starts_with(text: String, prefix: String) -> Bool
INFERREDfn(String, String) -> Bool

Check the beginning of the string without changing it.

Pooka
import pooka/string

assert string.starts_with("oakleaf", "oak")
function

ends_with

src/pooka/string.pooka:133
ends_with(text: String, suffix: String) -> Bool
INFERREDfn(String, String) -> Bool

Check the end of the string without changing it.

Pooka
import pooka/string

assert string.ends_with("oakleaf", "leaf")
function

replace

src/pooka/string.pooka:145
replace(text: String, old: String, replacement: String) -> String
INFERREDfn(String, String, String) -> String

Replace all nonoverlapping occurrences. An empty search string leaves the text unchanged.

Pooka
import pooka/string

assert string.replace("oak oak", "oak", "pine") == "pine pine"
function

trim

src/pooka/string.pooka:156
trim(text: String) -> String
INFERREDfn(String) -> String

Remove supported Unicode whitespace from both ends.

Pooka
import pooka/string

assert string.trim("  oak  ") == "oak"
function

trim_start

src/pooka/string.pooka:167
trim_start(text: String) -> String
INFERREDfn(String) -> String

Remove supported Unicode whitespace from the beginning only.

Pooka
import pooka/string

assert string.trim_start("  oak ") == "oak "
function

trim_end

src/pooka/string.pooka:178
trim_end(text: String) -> String
INFERREDfn(String) -> String

Remove supported Unicode whitespace from the end only.

Pooka
import pooka/string

assert string.trim_end(" oak  ") == " oak"
function

uppercase

src/pooka/string.pooka:189
uppercase(text: String) -> String
INFERREDfn(String) -> String

Apply the runtime's Unicode uppercase conversion.

Pooka
import pooka/string

assert string.uppercase("oak") == "OAK"
function

lowercase

src/pooka/string.pooka:200
lowercase(text: String) -> String
INFERREDfn(String) -> String

Apply the runtime's Unicode lowercase conversion.

Pooka
import pooka/string

assert string.lowercase("OAK") == "oak"
function

slice

src/pooka/string.pooka:212
slice(text: String, start: Int, count: Int) -> String
INFERREDfn(String, Int, Int) -> String

Take count code points after dropping start code points. Nonpositive bounds follow list drop/take semantics.

Pooka
import pooka/string

assert string.slice("🐇oak", 1, 2) == "oa"
function

repeat

src/pooka/string.pooka:227
repeat(text: String, count: Int) -> String
INFERREDfn(String, Int) -> String

Join count copies of the text. A nonpositive count produces an empty string.

Pooka
import pooka/string

assert string.repeat("ha", 3) == "hahaha"
function

to_list

src/pooka/string.pooka:240
to_list(text: String) -> List(Int)
INFERREDfn(String) -> List(Int)

Expose the Unicode scalar values as integer code points.

Pooka
import pooka/string

assert string.to_list("A") == [65]