Skip to content

Std\String

String – string manipulation and conversion.

Provides utilities for searching, transforming, splitting, and converting strings. Iterator-returning functions (split, lines, chars) use O(1) memory per element.

Returns the length of the string in bytes.

import length from Std\String in
length "hello" # => 5

Returns true if the string has zero length.

import isEmpty from Std\String in
isEmpty "" # => true
isEmpty "hi" # => false

Convert all characters to uppercase.

import toUpperCase from Std\String in
toUpperCase "hello" # => "HELLO"

Convert all characters to lowercase.

import toLowerCase from Std\String in
toLowerCase "HELLO" # => "hello"

Remove leading and trailing whitespace.

import trim from Std\String in
trim " hello " # => "hello"

Return the index of the first occurrence of needle, or -1 if not found.

import indexOf from Std\String in
indexOf "hello world" "world" # => 6

Returns true if str contains needle.

import contains from Std\String in
contains "hello world" "world" # => true

Returns true if str starts with prefix.

import startsWith from Std\String in
startsWith "hello" "hel" # => true

Returns true if str ends with suffix.

import endsWith from Std\String in
endsWith "hello.txt" ".txt" # => true

substring : String -> Int -> Int -> String

Section titled “substring : String -> Int -> Int -> String”

Extract a substring from index start (inclusive) to end (exclusive).

import substring from Std\String in
substring "hello" 1 4 # => "ell"

replace : String -> String -> String -> String

Section titled “replace : String -> String -> String -> String”

Replace all occurrences of old with new.

import replace from Std\String in
replace "aabaa" "a" "x" # => "xxbxx"

Split a string by a delimiter. Returns an Iterator String.

import split from Std\String in
let iter = split "a,b,c" "," in
# consume with iterator protocol

Join a sequence of strings with a separator.

import join from Std\String in
join ", " ["a", "b", "c"] # => "a, b, c"

Returns the character code (Int) at the given index.

import charAt from Std\String in
charAt "ABC" 0 # => 65

padLeft : Int -> String -> String -> String

Section titled “padLeft : Int -> String -> String -> String”

Pad str on the left with pad until it reaches width.

import padLeft from Std\String in
padLeft 5 "0" "42" # => "00042"

padRight : Int -> String -> String -> String

Section titled “padRight : Int -> String -> String -> String”

Pad str on the right with pad until it reaches width.

import padRight from Std\String in
padRight 5 "." "hi" # => "hi..."

Reverse a string.

import reverse from Std\String in
reverse "hello" # => "olleh"

Repeat a string n times.

import repeat from Std\String in
repeat 3 "ab" # => "ababab"

Take the first n characters of a string.

import take from Std\String in
take 3 "hello" # => "hel"

Drop the first n characters of a string.

import drop from Std\String in
drop 3 "hello" # => "lo"

Count the number of non-overlapping occurrences of needle in str.

import count from Std\String in
count "ababa" "ab" # => 2

Split a string into lines. Returns an Iterator String.

import lines from Std\String in
let iter = lines "a\nb\nc" in
# consume with iterator protocol

Join a sequence of strings with newline separators.

import unlines from Std\String in
unlines ["a", "b", "c"] # => "a\nb\nc"

Returns an Iterator over individual characters of the string.

import chars from Std\String in
let iter = chars "hello" in
# consume with iterator protocol

Build a string from a sequence of character codes.

import fromChars from Std\String in
fromChars [72, 105] # => "Hi"

Parse a string as an integer.

import toInt from Std\String in
toInt "42" # => 42

Parse a string as a float.

import toFloat from Std\String in
toFloat "3.14" # => 3.14