p-string
The string module provides utility functions for string manipulation.
use p-string as slen[str]
Section titled “len[str]”Returns the length of the string.
use p-string as ss.len[///hello///] -- 5contains[str; sub]
Section titled “contains[str; sub]”Returns true if str contains sub.
use p-string as ss.contains[///hello world///; ///world///] -- truestartswith[str; prefix]
Section titled “startswith[str; prefix]”Returns true if str starts with prefix.
endswith[str; suffix]
Section titled “endswith[str; suffix]”Returns true if str ends with suffix.
use p-string as ss.startswith[///hello///; ///he///] -- trues.endswith[///hello///; ///lo///] -- trueindexof[str; sub]
Section titled “indexof[str; sub]”Returns the index of the first occurrence of sub in str, or -1 if not found.
use p-string as ss.indexof[///hello///; ///ll///] -- 2s.indexof[///hello///; ///xyz///] -- -1count[str; sub]
Section titled “count[str; sub]”Returns the number of non-overlapping occurrences of sub in str.
use p-string as ss.count[///banana///; ///an///] -- 2Transform
Section titled “Transform”upper[str]
Section titled “upper[str]”Returns the string converted to uppercase.
lower[str]
Section titled “lower[str]”Returns the string converted to lowercase.
use p-string as ss.upper[///hello///] -- "HELLO"s.lower[///HELLO///] -- "hello"capitalize[str]
Section titled “capitalize[str]”Returns the string with the first character capitalized.
title[str]
Section titled “title[str]”Returns the string with the first character of each word capitalized.
use p-string as ss.capitalize[///hello world///] -- "Hello world"s.title[///hello world///] -- "Hello World"trim[str]
Section titled “trim[str]”Removes whitespace from both ends.
trimstart[str]
Section titled “trimstart[str]”Removes whitespace from the start.
trimend[str]
Section titled “trimend[str]”Removes whitespace from the end.
use p-string as ss.trim[/// hello ///] -- "hello"s.trimstart[/// hello ///] -- "hello "s.trimend[/// hello ///] -- " hello"reverse[str]
Section titled “reverse[str]”Returns the reversed string.
use p-string as ss.reverse[///hello///] -- "olleh"repeat[str; n]
Section titled “repeat[str; n]”Returns the string repeated n times.
use p-string as ss.repeat[///ab///; 3] -- "ababab"replace[str; old; new]
Section titled “replace[str; old; new]”Replaces all occurrences of old with new.
replacefirst[str; old; new]
Section titled “replacefirst[str; old; new]”Replaces the first occurrence of old with new.
use p-string as ss.replace[///aabaa///; ///a///; ///x///] -- "xxbxx"s.replacefirst[///aabaa///; ///a///; ///x///] -- "xabaa"padstart[str; len; fill]
Section titled “padstart[str; len; fill]”Pads the start of the string to reach len. The fill character defaults to a space.
padend[str; len; fill]
Section titled “padend[str; len; fill]”Pads the end of the string to reach len. The fill character defaults to a space.
use p-string as ss.padstart[///42///; 5; ///0///] -- "00042"s.padend[///hi///; 5; ///.///] -- "hi..."Split / Join
Section titled “Split / Join”split[str; sep]
Section titled “split[str; sep]”Splits the string by sep and returns an array.
lines[str]
Section titled “lines[str]”Splits the string by newlines.
words[str]
Section titled “words[str]”Splits the string by whitespace, ignoring empty segments.
join[arr; sep]
Section titled “join[arr; sep]”Joins an array of strings with sep.
chars[str]
Section titled “chars[str]”Returns an array of individual characters.
use p-string as ss.split[///a,b,c///; ///,///] -- ["a", "b", "c"]s.words[///hello world///] -- ["hello", "world"]s.join[[///a///; ///b///]; ///-///] -- "a-b"s.chars[///hi///] -- ["h", "i"]slice[str; start; end]
Section titled “slice[str; start; end]”Returns a substring from start to end (exclusive).
charat[str; i]
Section titled “charat[str; i]”Returns the character at index i.
codeat[str; i]
Section titled “codeat[str; i]”Returns the Unicode code point at index i.
fromcode[code]
Section titled “fromcode[code]”Returns the character for the given Unicode code point.
use p-string as ss.slice[///hello///; 1; 3] -- "el"s.charat[///hello///; 0] -- "h"s.codeat[///A///; 0] -- 65s.fromcode[65] -- "A"