Esc
Start typing to search...

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 Int
  • toFloat : String -> Maybe Float
  • charAt : Int -> String -> Maybe String

Functions

Querying

String.length

String -> Int

Get the length of a string in Unicode characters.

Example:
import String

String.length "hello"

-- 5
Try it

Notes: Counts Unicode code points, not bytes.

See also: String.isEmpty

String.isEmpty

String -> Bool

Check if a string is empty.

Example:
import String

String.isEmpty ""
Try it

See also: String.length

String.contains

String -> String -> Bool

Check if the first string contains the second string as a substring.

Example:
import String

String.contains "hello world" "world"

-- True
Try it

See also: String.startsWith, String.endsWith

String.startsWith

String -> String -> Bool

Check if the first string starts with the second string.

Example:
import String

String.startsWith "hello world" "hello"

-- True
Try it

See also: String.endsWith, String.contains

String.endsWith

String -> String -> Bool

Check if the first string ends with the second string.

Example:
import String

String.endsWith "hello world" "world"

-- True
Try it

See also: String.startsWith, String.contains

Splitting

String.split

String -> String -> [String]

Split a string by a delimiter.

Example:
import String

String.split "a,b,c" ","

-- ["a", "b", "c"]
Try it

See also: String.join, String.lines, String.words

String.lines

String -> [String]

Split a string into lines.

Example:
import String
String.lines "a\nb\nc"
Try it

Notes: 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).

Example:
import String

String.words "hello  world"

-- ["hello", "world"]
Try it

Notes: 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.

Example:
import String

String.toList "abc"

-- ["a", "b", "c"]
Try it

See also: String.fromList

Joining

String.join

String -> [String] -> String

Join a list of strings with a separator.

Example:
import String

String.join ", "
["a", "b", "c"]

-- "a, b, c"
Try it

See also: String.split, String.concat

String.unlines

[String] -> String

Join a list of strings with newlines.

Example:
import String
String.unlines ["a", "b", "c"]
Try it

See also: String.lines, String.join

String.unwords

[String] -> String

Join a list of strings with spaces.

Example:
import String

String.unwords ["hello", "world"]

-- "hello world"
Try it

See also: String.words, String.join

String.concat

[String] -> String

Concatenate a list of strings.

Example:
import String

String.concat ["hello", " ", "world"]

-- "hello world"
Try it

See also: String.join, String.fromList

String.fromList

[String] -> String

Convert a list of strings to a single string (concatenation).

Example:
import String

String.fromList ["a", "b", "c"]

-- "abc"
Try it

See also: String.toList, String.concat

Transforming

String.toUpper

String -> String

Convert a string to uppercase.

Example:
import String

String.toUpper "hello"

-- "HELLO"
Try it

Notes: Handles Unicode properly.

See also: String.toLower

String.toLower

String -> String

Convert a string to lowercase.

Example:
import String

String.toLower "HELLO"

-- "hello"
Try it

Notes: Handles Unicode properly.

See also: String.toUpper

String.trim

String -> String

Remove leading and trailing whitespace from a string.

Example:
import String

String.trim "  hello  "

-- "hello"
Try it

See also: String.trimLeft, String.trimRight

String.trimLeft

String -> String

Remove leading whitespace from a string.

Example:
import String

String.trimLeft "  hello  "

-- "hello  "
Try it

See also: String.trim, String.trimRight

String.trimRight

String -> String

Remove trailing whitespace from a string.

Example:
import String

String.trimRight "  hello  "

-- "  hello"
Try it

See also: String.trim, String.trimLeft

String.replace

String -> String -> String -> String

Replace all occurrences of a pattern with a replacement.

Example:
import String

String.replace "world" "Keel" "hello world"

-- "hello Keel"
Try it

Notes: Replaces all occurrences, not just the first.

String.reverse

String -> String

Reverse a string.

Example:
import String

String.reverse "hello"

-- "olleh"
Try it

See also: List.reverse

Slicing

String.slice

Int -> Int -> String -> String

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

Example:
import String

String.slice 1 4 "hello"

-- "ell"
Try it

Notes: 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.

Example:
import String

String.charAt 1 "hello"

-- Just "e"
Try it

See 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.

Example:
import String

String.padLeft 5 "0" "42"

-- "00042"
Try it

Notes: 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.

Example:
import String

String.padRight 5 "_" "hi"

-- "hi___"
Try it

Notes: Uses first character of padding string.

See also: String.padLeft

String.repeat

Int -> String -> String

Repeat a string n times.

Example:
import String

String.repeat 3 "ab"

-- "ababab"
Try it

Notes: Returns empty string if n <= 0.

See also: List.repeat

Converting

String.toInt

String -> Maybe Int

Parse a string as an integer.

Example:
import String

String.toInt "42"
Try it

Notes: Whitespace is trimmed before parsing.

See also: String.fromInt, String.toFloat

String.toFloat

String -> Maybe Float

Parse a string as a float.

Example:
import String

String.toFloat "3.14"
Try it

Notes: Whitespace is trimmed before parsing.

See also: String.fromFloat, String.toInt

String.fromInt

Int -> String

Convert an integer to a string.

Example:
import String

String.fromInt 42

-- "42"
Try it

See also: String.toInt, String.fromFloat

String.fromFloat

Float -> String

Convert a float to a string.

Example:
import String

String.fromFloat 3.14

-- "3.14"
Try it

See also: String.toFloat, String.fromInt