String Module
Functions for working with strings — text processing and conversions.
Strings in Keel are immutable UTF-8 text. The String type is a primitive — there is no separate Char sequence. String functions return new strings rather than modifying in place.
Common patterns
import String
-- Split and join
String.split "," "a,b,c" -- ["a", "b", "c"]
String.join ", " ["x", "y", "z"] -- "x, y, z"
-- Transform with pipes
" Hello, World! "
|> String.trim
|> String.toUpper -- "HELLO, WORLD!"
-- Slice and pad
String.slice 0 5 "Hello, World!" -- "Hello"
String.padLeft 8 "0" "42" -- "00000042"
-- Safe parsing (returns Maybe)
String.toInt "42" -- Just 42
String.toInt "abc" -- Nothing
String.toFloat "3.14" -- Just 3.14
-- Line and word processing
String.words "hello world" -- ["hello", "world"]
String.lines "a\nb\nc" -- ["a", "b", "c"]
Return types
Functions that can fail return Maybe instead of crashing:
toInt : String -> Maybe InttoFloat : String -> Maybe FloatcharAt : Int -> String -> Maybe String
Functions
Querying
String.length
String -> Int
Get the length of a string in Unicode characters.
import String
String.length "hello"
-- 5Try itNotes: Counts Unicode code points, not bytes.
See also: String.isEmpty
String.isEmpty
String -> Bool
Check if a string is empty.
import String
String.isEmpty ""Try itSee also: String.length
String.contains
String -> String -> Bool
Check if the first string contains the second string as a substring.
import String
String.contains "hello world" "world"
-- TrueTry itSee also: String.startsWith, String.endsWith
String.startsWith
String -> String -> Bool
Check if the first string starts with the second string.
import String
String.startsWith "hello world" "hello"
-- TrueTry itSee also: String.endsWith, String.contains
String.endsWith
String -> String -> Bool
Check if the first string ends with the second string.
import String
String.endsWith "hello world" "world"
-- TrueTry itSee also: String.startsWith, String.contains
Splitting
String.split
String -> String -> [String]
Split a string by a delimiter.
import String
String.split "a,b,c" ","
-- ["a", "b", "c"]Try itSee also: String.join, String.lines, String.words
String.lines
String -> [String]
Split a string into lines.
import String
String.lines "a\nb\nc"Try itNotes: Handles both \n and \r\n line endings.
See also: String.unlines, String.split
String.words
String -> [String]
Split a string into words (split by whitespace).
import String
String.words "hello world"
-- ["hello", "world"]Try itNotes: Multiple spaces are treated as a single separator.
See also: String.unwords, String.split
String.toList
String -> [String]
Convert a string to a list of single-character strings.
import String
String.toList "abc"
-- ["a", "b", "c"]Try itSee also: String.fromList
Joining
String.join
String -> [String] -> String
Join a list of strings with a separator.
import String
String.join ", "
["a", "b", "c"]
-- "a, b, c"Try itSee also: String.split, String.concat
String.unlines
[String] -> String
Join a list of strings with newlines.
import String
String.unlines ["a", "b", "c"]Try itSee also: String.lines, String.join
String.unwords
[String] -> String
Join a list of strings with spaces.
import String
String.unwords ["hello", "world"]
-- "hello world"Try itSee also: String.words, String.join
String.concat
[String] -> String
Concatenate a list of strings.
import String
String.concat ["hello", " ", "world"]
-- "hello world"Try itSee also: String.join, String.fromList
String.fromList
[String] -> String
Convert a list of strings to a single string (concatenation).
import String
String.fromList ["a", "b", "c"]
-- "abc"Try itSee also: String.toList, String.concat
Transforming
String.toUpper
String -> String
Convert a string to uppercase.
import String
String.toUpper "hello"
-- "HELLO"Try itNotes: Handles Unicode properly.
See also: String.toLower
String.toLower
String -> String
Convert a string to lowercase.
import String
String.toLower "HELLO"
-- "hello"Try itNotes: Handles Unicode properly.
See also: String.toUpper
String.trim
String -> String
Remove leading and trailing whitespace from a string.
import String
String.trim " hello "
-- "hello"Try itSee also: String.trimLeft, String.trimRight
String.trimLeft
String -> String
Remove leading whitespace from a string.
import String
String.trimLeft " hello "
-- "hello "Try itSee also: String.trim, String.trimRight
String.trimRight
String -> String
Remove trailing whitespace from a string.
import String
String.trimRight " hello "
-- " hello"Try itSee also: String.trim, String.trimLeft
String.replace
String -> String -> String -> String
Replace all occurrences of a pattern with a replacement.
import String
String.replace "world" "Keel" "hello world"
-- "hello Keel"Try itNotes: Replaces all occurrences, not just the first.
String.reverse
String -> String
Reverse a string.
import String
String.reverse "hello"
-- "olleh"Try itSee also: List.reverse
Slicing
String.slice
Int -> Int -> String -> String
Get a substring from start index (inclusive) to end index (exclusive).
import String
String.slice 1 4 "hello"
-- "ell"Try itNotes: Indices are clamped to valid range.
See also: String.charAt
String.charAt
Int -> String -> Maybe String
Get the character at the given index as a single-character string.
import String
String.charAt 1 "hello"
-- Just "e"Try itSee also: String.slice, List.nth
String.padLeft
Int -> String -> String -> String
Pad a string on the left to the given length with the padding string.
import String
String.padLeft 5 "0" "42"
-- "00042"Try itNotes: Uses first character of padding string.
See also: String.padRight
String.padRight
Int -> String -> String -> String
Pad a string on the right to the given length with the padding string.
import String
String.padRight 5 "_" "hi"
-- "hi___"Try itNotes: Uses first character of padding string.
See also: String.padLeft
String.repeat
Int -> String -> String
Repeat a string n times.
import String
String.repeat 3 "ab"
-- "ababab"Try itNotes: Returns empty string if n <= 0.
See also: List.repeat
Converting
String.toInt
String -> Maybe Int
Parse a string as an integer.
import String
String.toInt "42"Try itNotes: Whitespace is trimmed before parsing.
See also: String.fromInt, String.toFloat
String.toFloat
String -> Maybe Float
Parse a string as a float.
import String
String.toFloat "3.14"Try itNotes: Whitespace is trimmed before parsing.
See also: String.fromFloat, String.toInt
String.fromInt
Int -> String
Convert an integer to a string.
import String
String.fromInt 42
-- "42"Try itSee also: String.toInt, String.fromFloat
String.fromFloat
Float -> String
Convert a float to a string.
import String
String.fromFloat 3.14
-- "3.14"Try itSee also: String.toFloat, String.fromInt