Skip to content

p-string

The string module provides utility functions for string manipulation.

use p-string as s

Returns the length of the string.

use p-string as s
s.len[///hello///] -- 5

Returns true if str contains sub.

use p-string as s
s.contains[///hello world///; ///world///] -- true

Returns true if str starts with prefix.

Returns true if str ends with suffix.

use p-string as s
s.startswith[///hello///; ///he///] -- true
s.endswith[///hello///; ///lo///] -- true

Returns the index of the first occurrence of sub in str, or -1 if not found.

use p-string as s
s.indexof[///hello///; ///ll///] -- 2
s.indexof[///hello///; ///xyz///] -- -1

Returns the number of non-overlapping occurrences of sub in str.

use p-string as s
s.count[///banana///; ///an///] -- 2

Returns the string converted to uppercase.

Returns the string converted to lowercase.

use p-string as s
s.upper[///hello///] -- "HELLO"
s.lower[///HELLO///] -- "hello"

Returns the string with the first character capitalized.

Returns the string with the first character of each word capitalized.

use p-string as s
s.capitalize[///hello world///] -- "Hello world"
s.title[///hello world///] -- "Hello World"

Removes whitespace from both ends.

Removes whitespace from the start.

Removes whitespace from the end.

use p-string as s
s.trim[/// hello ///] -- "hello"
s.trimstart[/// hello ///] -- "hello "
s.trimend[/// hello ///] -- " hello"

Returns the reversed string.

use p-string as s
s.reverse[///hello///] -- "olleh"

Returns the string repeated n times.

use p-string as s
s.repeat[///ab///; 3] -- "ababab"

Replaces all occurrences of old with new.

Replaces the first occurrence of old with new.

use p-string as s
s.replace[///aabaa///; ///a///; ///x///] -- "xxbxx"
s.replacefirst[///aabaa///; ///a///; ///x///] -- "xabaa"

Pads the start of the string to reach len. The fill character defaults to a space.

Pads the end of the string to reach len. The fill character defaults to a space.

use p-string as s
s.padstart[///42///; 5; ///0///] -- "00042"
s.padend[///hi///; 5; ///.///] -- "hi..."

Splits the string by sep and returns an array.

Splits the string by newlines.

Splits the string by whitespace, ignoring empty segments.

Joins an array of strings with sep.

Returns an array of individual characters.

use p-string as s
s.split[///a,b,c///; ///,///] -- ["a", "b", "c"]
s.words[///hello world///] -- ["hello", "world"]
s.join[[///a///; ///b///]; ///-///] -- "a-b"
s.chars[///hi///] -- ["h", "i"]

Returns a substring from start to end (exclusive).

Returns the character at index i.

Returns the Unicode code point at index i.

Returns the character for the given Unicode code point.

use p-string as s
s.slice[///hello///; 1; 3] -- "el"
s.charat[///hello///; 0] -- "h"
s.codeat[///A///; 0] -- 65
s.fromcode[65] -- "A"